2018.1.1 Spring Boot 2 Tests Jackson Serialization issue HttpMessageNotWritableException
Answered
I have made a simple model class
public final class DomainResponse {
private String content;
private Code code;
private DomainResponse() {
}
public static DomainResponse of(final String content, final Code code) {
DomainResponse dr = new DomainResponse();
dr.content = content;
dr.code = code;
return dr;
}
public Code getCode() {
return code;
}
public void setCode(final Code code) {
this.code = code;
}
}
Along with a Controller and Service class and have written the following test class:
@RunWith(SpringRunner.class)
@WebMvcTest(DomainController.class)
public class DomainControllerContractTest {
@MockBean
private DomainService service;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnDefaultMessage() throws Exception {
when(service.serveWithName(any(String.class))).thenReturn(DomainResponse.of("Hello World", Code.OK));
this.mockMvc.perform(get("/domain")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("{\"code\":\"OK\"}")));
}
}
when I perform `gradle clean build` in the terminal, the test passes, so the `DomainResponse` class is being serialized
but when I run it in IntelliJ IDEA, the test fails with the exception:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class au.com.auspost.myapi.domain.DomainResponse
I imagine that there is some IDE-based implementation of Jackson getting in the way, but I can't find it, can anyone help with this?
Please sign in to leave a comment.
Do you use JUnit Run/Debug Configuration or Gradle one to run test? Do you use Delegate IDE build/run actions to Gradle in Gradle | Runner settings? Does it help to Enable/Disable this option and make project rebuild or use different Run/Debug Configuration (Gradle or JUnit one) to run test?
Check also if you have all the dependencies defined: https://stackoverflow.com/q/37841373/2000323 and their scope is correct.
If issue remains, can you provide a sample project to reproduce? Thanks.
In terms of the code, all the dependencies are correct because the same code runs perfectly when I use the command line to run a gradle build with the Tests included.
The problem seems to be when I try to run one of the tests inside IntelliJ. I right-click on the Test class and run it. I assume that this is using JUnit run/debug. I do have the Delegate IDE build/run actions to gradle set, so I guess somehow by doing the Run/Debug choice, I don't have the right things in the classpath.
I'll get a project together to reproduce.