有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    以下是一些你应该寻找的东西:

    • 使用“debug”启动应用程序,或者如果使用Log4J/Logback,则激活调试日志,并在日志中查找RestController的注册信息
    • 检查你的申请。属性或应用程序。yml表示server.servlet-pathserver.context-path
    • 如果你有网络。xml检查Spring DispatcherServlet的servlet映射