rest java。lang.AssertionError:状态应为:<200>,但在restful服务中为:<404>
我试图在下面对我的程序进行单元测试,但得到的错误是:java。lang.AssertionError:预期状态:<;200>;但事实是:<;404>
测试等级:
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());
}}
控制器:
@RestController
public class HelloController {
@RequestMapping("newcontroller")
public ModelAndView firstPage() {
System.out.println(" Inside Hello Controller ....");
return new ModelAndView("index");
}}
# 1 楼答案
以下是一些你应该寻找的东西:
server.servlet-path
和server.context-path
李>