User23
User23

Reputation: 21

java.lang.AssertionError: Status expected:<200> but was:<400> in junit

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: 37133

Answers (2)

Atul
Atul

Reputation: 3367

The one way to solve is to add(mvc:annotation-driven):

in your xml file where you have mentioned all the spring configuration.

When you execute the test case, the logs said that the given url pattern it wont find. Even the logs said it not found controller.

Might be this will help to someone.

Upvotes: 0

dubes
dubes

Reputation: 5524

HTTP 400 Stands for Bad Request, which as per the specification should be returned when:

The request could not be understood by the server due to malformed syntax.

Since in your controller you have defined: @RequestParam(value = "videoid", required = true) String videoId) and in your test you are passing videoId, spring is not able to match the videoid which is a required parameter and thus raises 400 error.

Please note, that the parameter in your request must match what you have defined as value in the RequestParam and not the name of the parameter.

Upvotes: 2

Related Questions