我无法模拟我的 getUserCards 返回空列表。它总是从数据库中获取信息。你有什么想法?我附上我的 get 请求和 mockito 实现。我想设置 getUserCards 方法总是返回一个空列表,但我不能被嘲笑
@Before
public void setUp() throws Exception{
//cards.add(new Card());
CardService mock = Mockito.mock(CardService.class);
Mockito.when(mock.getUserCards("1234"))
.thenReturn(cards);
}
@Test
public void testMockCards() throws Exception{
mockMvc = MockMvcBuilders.standaloneSetup(cardController).build();
MockHttpServletResponse response = mockMvc.perform(
MockMvcRequestBuilders.get("/cards")
)
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse();
ObjectMapper mapper = new ObjectMapper();
List<Card> cardResponse = mapper.readValue(response.getContentAsString(),
new TypeReference<List<Card>>(){});
Assert.assertEquals(cards, cardResponse);
}
得到
@RequestMapping(value = "/cards", method = RequestMethod.GET)
@JsonView(Views.Public.class)
public List<Card> getUserCards(
{
session.getEntityUserId());
}
当年话下
相关分类