1,引入Spring测试jarspring-test-4.1.6.RELEASE
2,单元测试Service如下:
package com.order.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.jutudi.common.result.GridResult;
import com.jutudi.order.dao.pojo.OrderBuyer;
import com.jutudi.order.service.OrderBuyerService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext*.xml")
@Transactional
publicclassOrderBuyerServiceTest{
@Autowired
privateOrderBuyerService service;
@Test
publicvoid testQueryAll()throwsException{
GridResult<OrderBuyer> grid = service.querySellOrders(newOrderBuyer(),1,10);
System.out.println("测试"+ grid.toString());
}
}
3,单元测试Controller
?具体内容,参考官方文档:
http://www.cnblogs.com/naruto469/p/4087821.html 出错后参考博客
/**
* 此处必须引入,否则报错status() get() 方法找不到
*/
importstatic org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
/**
* 此处必须引入,否则报错status() get() 方法找不到
*/
importstatic org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:spring/applicationContext*.xml")
//@EnableWebMvc
//@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:spring/applicationContext*.xml")
@EnableWebMvc
publicclassAppFundControllerTest{
@Autowired
privateWebApplicationContext wac;
privateMockMvc mockMvc;
privatestaticfinalLogger log =LoggerFactory.getLogger(AppFundControllerTest.class);
@Before
publicvoid setup()throwsException{
this.mockMvc =MockMvcBuilders.webAppContextSetup(this.wac).build();
// this.mockMvc = MockMvcBuilders.standaloneSetup(new AppFundController()).build();
}
@Test
publicvoid testReceive()throwsException{
// 1、mockMvc.perform执行一个请求;
// 2、MockMvcRequestBuilders.get("/user/1")构造一个请求
// 3、ResultActions.andExpect添加执行完成后的断言
// 4、ResultActions.andDo添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
// 5、ResultActions.andReturn表示执行完成后返回相应的结果。
// MvcResult result =
// mockMvc.perform(MockMvcRequestBuilders.get("/jutudiApp/appfindtopfundclass.html"))
// // .andExpect(MockMvcResultMatchers.view().name("user/view"))
// // .andExpect(MockMvcResultMatchers.model().attributeExists("user"))
// // .andDo(MockMvcResultHandlers.print())
// .andReturn();
MvcResult result =this.mockMvc
.perform(get("/jutudiApp/appfindtopfundclass.html").accept(MediaType.ALL).param("id","256"))
.andExpect(status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();
log.debug("查询结果: "+ result.getResponse().getHeader("Content-Type"));
}
}
时间: 2024-10-15 09:53:57