0
我得到一個空的JSON {}
(通過actions.andReturn().getResponse().getContentAsString()
驗證),當我通過mockMvc.perform(post...))
測試儘管的方法實際上返回響應(我看到這個的時候我調試和單步執行代碼,這是一個有效的,充滿響應的響應對象,但是當mockito中的某些代碼正在製作modalAndView時突然變爲空 - 爲什麼它會這樣做?)。爲什麼@InjectMocks控制器的響應在我使用@JsonView時被Mockito變爲空?
測試類:
@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());
}
}
你可以顯示你的代碼中結果得到'null'的部分嗎? – SilverNak
@SilverNak結果在我的代碼中不會爲空。這是當我調試和步入Mockito的代碼,它突然這樣做 –
因此,'actions.andReturn()。getResponse()。getContentAsString()'確實返回一個值? – SilverNak