Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No serializer found while using mockito with Java 11

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>
like image 462
MatWdo Avatar asked Mar 06 '23 00:03

MatWdo


1 Answers

I fix this test using real object not mocked dto's

like image 173
MatWdo Avatar answered Mar 21 '23 03:03

MatWdo