为什么当我使用 @JsonView 时,@InjectMocks 控制器的响应会被 Mockito 变为空?

Why does @InjectMocks controller's response get turned into empty by Mockito when I use @JsonView?

当我通过 mockMvc.perform(post...)) 尽管[=19 进行测试时,我得到一个空的 json {}(通过 actions.andReturn().getResponse().getContentAsString() 验证)作为响应=] 实际上返回响应的方法(我在调试和单步执行代码时看到了这一点。它是一个有效的、填充的响应对象,但是当 mockito 中的某些代码正在制作 modalAndView 时突然变为 null - 为什么要这样做?)。

测试class:

@RunWith(MockitoJUnitRunner.class)
public class Test
{
    //Also tried @Mock
    @InjectMocks
    private MyService myService;

    @Mock
    private MyDAO myDAO;

    //@Autowired
    @InjectMocks
    private MyController myController;

    @Before
    public void setup() {

        //Build the controller mock handler
        mockMvc = MockMvcBuilders
            .standaloneSetup(myController)
            .setControllerAdvice(new ExceptionHandler())
            .build();
    }

    @org.junit.Test
    public void testMyEndpoint() throws Exception
    {
        //Make a request object
        MyRequest request = readJson("request.json", MyRequest.class );

        List<MyObject> objects = readJson("data.json", MyObject.class );

        Mockito.when(
            myDAO.getData( request )
        ).thenReturn(objects);

        Mockito.when(
            myService.callDAO(request)
        )
            .thenReturn(objects)

        //Call the aum endpoint
        ResultActions actions = mockMvc.perform(
            post( "/v1/endpoint" )
                .contentType(MediaType.APPLICATION_JSON)
                .content( new ObjectMapper().writeValueAsString( request ) )
        );

        //Why is this empty?
        System.out.println( actions.andReturn().getResponse().getContentAsString() );
    }
}

Mockito 使用了一个不理解 @JsonView 的 ObjectMapper。要解决这个问题,您需要设置一个消息转换器。

@RunWith(MockitoJUnitRunner.class)
public class Test
{
    //Also tried @Mock
    @InjectMocks
    private MyService myService;

    @Mock
    private MyDAO myDAO;

    //@Autowired
    @InjectMocks
    private MyController myController;

    /**
     * This is required for JsonViews.
     * @return
     */
    public static MappingJackson2HttpMessageConverter createJacksonConverter() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper);
        return converter;
    }

    @Before
    public void setup() {

        //Build the controller mock handler
        mockMvc = MockMvcBuilders
            .standaloneSetup(myController)
            .setControllerAdvice(new ExceptionHandler())
            .setMessageConverters(createJacksonConverter())
            .build();
    }

    @org.junit.Test
    public void testMyEndpoint() throws Exception
    {
        //Make a request object
        MyRequest request = readJson("request.json", MyRequest.class );

        List<MyObject> objects = readJson("data.json", MyObject.class );

        Mockito.when(
            myDAO.getData( request )
        ).thenReturn(objects);

        Mockito.when(
            myService.callDAO(request)
        )
            .thenReturn(objects)

        //Call the aum endpoint
        ResultActions actions = mockMvc.perform(
            post( "/v1/endpoint" )
                .contentType(MediaType.APPLICATION_JSON)
                .content( new ObjectMapper().writeValueAsString( request ) )
        );

        //Why is this empty?
        System.out.println( actions.andReturn().getResponse().getContentAsString() );
    }
}