Issue
I created a test for my controller and service using mockito. While I use java 8 everything is Ok, but after migration to Java 11, I have this error. Maybe someone can help me fix this test?
public class ConfigurationControllerTest {
@Mock
private ConfigurationService configurationService;
@InjectMocks
private ConfigurationController controller;
private MockMvc mockMvc;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(controller)
.build();
}
@Test
public void find() throws Exception {
//given
final ConfigurationsDto configDto = mock(ConfigurationsDto.class);
given(configurationService.find(any())).willReturn(Arrays.asList(configDto));
//when and then
mockMvc
.perform(get("/config/1.0.0/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
After run, I recived https status 500 and error:
[main] ERROR app.controllers.global.AdviceController - No serializer found for class org.mockito.internal.debugging.LocationImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.Collections$SingletonList[0]->app.dtos.ConfigurationsDto$MockitoMock$534475338["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]->org.mockito.internal.handler.InvocationNotifierHandler["invocationContainer"]->org.mockito.internal.stubbing.InvocationContainerImpl["invocationForStubbing"]->org.mockito.internal.invocation.InvocationMatcher["invocation"]->org.mockito.internal.invocation.InterceptedInvocation["location"])
Of course, I add the needed dependency in pom:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
</dependency>
Solution
I fix this test using real object not mocked dto's
Answered By - MatWdo
Answer Checked By - Pedro (JavaFixing Volunteer)