【发布时间】:2017-02-16 12:13:06
【问题描述】:
这是我的控制器...
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/categories")
public POSResponse getAllCategories() {
String countryCode="1";
return infoService.getAllCategories(countryCode);
}
这是我的 testController....
@Mock
InfoService infoService;
@InjectMocks
private InfoController infoController;
private MockMvc mockMvc;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(infoController).build();
}
@Test
public void getAllCategoriesTest() throws Exception {
POSResponse response=new POSResponse();
Category category=new Category();
category.setCountryCode(1);
category.setDescription("Mother Dairy");
response.setResponse(category);
when(infoService.getAllCategories("1")).thenReturn(response);
mockMvc.perform(get("/categories"))
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.id", is(1)))
.andExpect(jsonPath("$.description", is("Mother Dairy")));
verify(infoService, times(1)).getAllCategories("1");
verifyNoMoreInteractions(infoService);
}
我正在使用球衣控制器。 当我调用该方法时,我收到错误消息“java.lang.AssertionError: Status expected: but was:”
【问题讨论】:
-
你确定
infoService被注入你的控制器了吗?在我看来,您必须先致电MockMvcBuilders.standaloneSetup,然后再致电MockitoAnnotations.initMocks