spring-bootspring-cloud-contractcontract

Spring Cloud Contract Content type not supported


I am using spring cloud contract and I am getting the error

    2020-06-23 23:27:41.940  WARN 39531 --- 
    [main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: 
    Content type 'application/json;charset=ISO-8859-1' not supported]

I am not sure if I am missing anything. I have tried several things and read the documentation several times and I cannot find the issue. I have also successfully used spring cloud contracts in the past (In my old job, don't have the code anymore) Maybe there is a problem of compatibility with the newer versions? Here are the classes I am using:

Controller

    @RestController
    @RequestMapping(value = "/names")
    public class NameController {
    
        @PostMapping
        public Name addName(@RequestBody Name name) {
            return name;
        }
    }

Entity (Name)

    @Data
    public class Name {
    
        private Long id;
        private String name;
    
    }

BaseClass for set up

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
    @DirtiesContext
    @AutoConfigureMessageVerifier
    public class BaseClass {
    
        @Autowired
        private NameController nameController;
    
        @BeforeEach
        void setUp() {
    
            StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(nameController);
            RestAssuredMockMvc.standaloneSetup(builder);
        }
    }

create_name.groovy

    Contract.make {
    
        request {
            url '/names'
            method 'POST'
            headers {
                contentType(applicationJson())
            }
            body([
                    id  : 1,
                    name: "hello"
            ])
        }
        response {
            status OK()
        }
    }

Also here is the generated test

    @SuppressWarnings("rawtypes")
    public class ContractVerifierTest extends BaseClass {
    
        @Test
        public void validate_create_name() throws Exception {
            // given:
                MockMvcRequestSpecification request = given()
                        .header("Content-Type", "application/json")
                        .body("{\"id\":1,\"name\":\"hello\"}");
    
            // when:
                ResponseOptions response = given().spec(request)
                        .post("/names");
    
            // then:
                assertThat(response.statusCode()).isEqualTo(200);
        }
    }

Solution

  • I should have started my search in the Github repository. This seems to be a known issue here is the link: https://github.com/spring-cloud/spring-cloud-contract/issues/1428

    Here is how my working test base class looks.

        @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
        public class BaseClass {
        
            @Autowired
            private NameController nameController;
        
            @BeforeEach
            void setUp() {
        
                EncoderConfig encoderConfig = new EncoderConfig(Charsets.UTF_8.name(), Charsets.UTF_8.name());
                RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().encoderConfig(encoderConfig);
        
                StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(nameController);
        
                RestAssuredMockMvc.standaloneSetup(builder);
            }
        }