Reputation: 21
it's my controller...
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/categories")
public POSResponse getAllCategories() {
String countryCode="1";
return infoService.getAllCategories(countryCode);
}
it's my 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);
}
i am using jersey controller. when i call the method i got error msg"java.lang.AssertionError: Status expected:<200> but was:<400>"
Upvotes: 1
Views: 1422
Reputation: 3141
Whether you may use in your controller :
@Consumes(MediaType.APPLICATION_JSON) // instead of MediaType.APPLICATION_FORM_URLENCODED
Or, in you test :
mockMvc.perform(get("/categories")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE))
...
Why?
An HTTP request should be in one of the media types accepted by the server, and MockMvc might use MediaType.APPLICATION_JSON (As my test show!). You can check it by printing the request detail:
mockMvc.perform(get("/categories")
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andDo(MockMvcResultHandlers.print())
...
Upvotes: 1