运行测试类,报出如下错误,该怎么解决啊,谢谢前辈指导
错误如下:
java.lang.AssertionError: Status
Expected :200
Actual :404
<Click to see difference>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:655)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
at ContorllerDemo.demo1(ContorllerDemo.java:33)
测试类如下:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:applicationContext.xml")
public class ContorllerDemo {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void before(){
mockMvc = MockMvcBuilders.webAppContextSetup( webApplicationContext ).build();
}
@Test
public void demo1() throws Exception{
String responseString = mockMvc.perform(get("manager/bannerLibary/select.html"))
.andExpect(status().isOk()).andDo(print()) //打印出请求和相应的内容
.andReturn().getResponse().getContentAsString();
System.out.println(responseString);
}
}
controller类如下:
@Controller
@RequestMapping("manager/bannerLibary")
public class BannerLibaryController {
@Autowired
BannerLibaryService bannerLibaryService;
@Autowired
BannerLibaryDao bannerLibaryDao;
/**
* 获取BannerLibary
*
* @param bannerLibary 条件
* @param response
* @Return
*/
@RequestMapping("select")
public void select(BannerLibary bannerLibary, HttpServletResponse response) {
List<BannerLibary> bannerLibaries = bannerLibaryDao.select(bannerLibary);
Map m = new HashMap();
m.put("code", 0);
m.put("count", bannerLibaries.size());
if (bannerLibaries == null || bannerLibaries.size() <= 0) {
m.put("msg", "没有结果");
m.put("result", "");
} else {
m.put("msg", "");
m.put("res_code", "1111");
m.put("data", bannerLibaries);
}
String json = JsonUtil.ObjectToJson(m);
TaokeUtil.write(json, response);
}
}
springmvc配置如下:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
在这个方法里TaokeUtil.write(json, response); 你都对response做了些啥啊?