简体   繁体   中英

java.lang.AssertionError: Status expected:<200> but was:<404> in restful services

I am trying to unit test my program below but getting error as: java.lang.AssertionError: Status expected:<200> but was:<404>

Test Class:

package com.hsbc.mvc;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppContext.class})
@WebAppConfiguration
public class ControllerTest {

@Autowired
private WebApplicationContext context;

private MockMvc mvc;


@Before
public void before() {
    MockitoAnnotations.initMocks(this);
    this.mvc = MockMvcBuilders.webAppContextSetup(this.context).dispatchOptions(true).build();
}

@Test
public void testMyMvcController() throws Exception {

    this.mvc.perform(get("/newcontroller")).andExpect(status().isOk());

}}

Controller:

@RestController

public class HelloController {

@RequestMapping("newcontroller")
public ModelAndView firstPage() {
    System.out.println(" Inside Hello Controller ....");
    return new ModelAndView("index");
}}

Here are some things you should be looking for:

  • Start your application with "--debug" or if you are using Log4J / Logback activate DEBUG logs, and look in the logs for the registration of your RestController.
  • Check your application.properties or application.yml for server.servlet-path and server.context-path .
  • If you have a web.xml check for the servlet mapping of the Spring DispatcherServlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM