【发布时间】:2017-06-13 09:37:08
【问题描述】:
我正在尝试在下面对我的程序进行单元测试,但出现错误:java.lang.AssertionError: Status expected: but was:
测试类:
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");
}}
【问题讨论】:
-
您可能需要在@RequestMapping("newcontroller") 中使用前导斜杠
-
你用错了标签,用spring-boot代替boot。
-
@mikep 也不能使用斜线
-
如何开始您的应用程序?您使用外部 tomcat 还是嵌入式?也许你有一些像
/yourApplication/newcontroller这样的前缀?有什么安全感吗?也许试试@RequestMapping("newcontroller", method = RequestMethod.GET)) -
@lazarov 我正在使用 spring-boot 来运行应用程序,因此它在内部使用 tomcat。此外,当我直接通过网络浏览器运行它时,它可以工作..但对于单元测试用例它不起作用。我也试过@RequestMapping("newcontroller", method = RequestMethod.GET)),这也行不通
标签: java rest spring-boot