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:
server.servlet-path
and server.context-path
.
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.