java.lang.AssertionError:预期状态:<;200>;但事实是:<;404>;在宁静的服务中
我试图在下面对我的程序进行单元测试,但得到的错误为:java.lang.AssertionError:Status expected:但是是: 测试等级:java.lang.AssertionError:预期状态:<;200>;但事实是:<;404>;在宁静的服务中,java,rest,spring-boot,Java,Rest,Spring Boot,我试图在下面对我的程序进行单元测试,但得到的错误为:java.lang.AssertionError:Status expected:但是是: 测试等级: package com.hsbc.mvc; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebAppContext.class}) @WebAppConfiguration public class ControllerTest { @Au
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");
}}
以下是您应该寻找的一些东西:
- 使用“-debug”启动应用程序,或者如果使用Log4J/Logback,则激活调试日志,并在日志中查找RestController的注册
- 检查application.properties或application.yml中的
和server.servlet路径
server.context路径
- 如果您有web.xml,请检查Spring DispatcherServlet的servlet映射
/yourApplication/newcontroller
?有保安吗?也许可以尝试使用@RequestMapping(“newcontroller”,method=RequestMethod.GET))
@lazarov我正在使用springboot来运行应用程序,因此它在内部使用tomcat。而且,当我通过web浏览器直接运行它时,它也可以工作。。但是对于单元测试用例,它不起作用。我还尝试了@RequestMapping(“newcontroller”,method=RequestMethod.GET)),这也不起作用