简体   繁体   中英

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>

我使用真实对象而不是模拟dto来修复此测试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM