单元测试代码:SpringTest+JUnit

  1. <pre class="java" name="code">/** 
  2.  * JUnit单元测试父类,配置了Spring的基础环境。 <br/> 
  3.  * 可以作为Controller、Service、Dao单元测试的父类。 
  4.  *  
  5.  * @author leiwen@fansunion.cn 
  6.  */  
  7. public class JUnitTestBase {  
  8.   
  9.  public static XmlWebApplicationContext context = null;  
  10.   
  11.  private static HandlerMapping handlerMapping;  
  12.  private static HandlerAdapter handlerAdapter;  
  13.   
  14.  // /public static String[] CONFIG_FILES = { "classpath:spring-*.xml" };  
  15.   
  16.  // public static String[] configs = { "file:src/main/resources/spring-*.xml"  
  17.  // };  
  18.  public static String[] CONFIG_FILES = { "file:src/main/resources/spring-*.xml" };  
  19.   
  20.  /** 
  21.   * 读取spring配置文件,初始化上下文。 
  22.   */  
  23.  @BeforeClass  
  24.  public static void setUp() {  
  25.   System.out.println("Test start...");  
  26.   
  27.   context = new XmlWebApplicationContext();  
  28.   context.setConfigLocations(CONFIG_FILES);  
  29.   
  30.   MockServletContext msc = new MockServletContext();  
  31.   context.setServletContext(msc);  
  32.   context.refresh();  
  33.   msc.setAttribute(  
  34.     WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,  
  35.     context);  
  36.   
  37.   handlerMapping = (HandlerMapping) context  
  38.     .getBean(DefaultAnnotationHandlerMapping.class);  
  39.   handlerAdapter = (HandlerAdapter) context.getBean(context  
  40.     .getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);  
  41.   
  42.  }  
  43.   
  44.  // 执行request对象请求的action  
  45.  public ModelAndView excuteAction(HttpServletRequest request,  
  46.    HttpServletResponse response) throws Exception {  
  47.   // 这里需要声明request的实际类型,否则会报错  
  48.   request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);  
  49.   
  50.   HandlerExecutionChain chain = handlerMapping.getHandler(request);  
  51.   Object handler = chain.getHandler();  
  52.   final ModelAndView model = handlerAdapter.handle(request, response,  
  53.     handler);  
  54.   return model;  
  55.  }  
  56.   
  57.  @AfterClass  
  58.  public static void tearUp() {  
  59.   System.out.println("Test end!");  
  60.  }  
  61.   
  62. }  
  63.   
  64.    
  65.   
  66.   
  67.   
  68.   
  69. //测试Controller的2种方法  
  70.   
  71.    
  72.   
  73. /** 
  74.  * CampaignGroupController单元测试。 
  75.  *  
  76.  * @author leiwen@fansunion.cn 
  77.  */  
  78. public class FansUnionControllerTestextends JUnitTestBase {  
  79.   
  80.  @Test  
  81.  public void prevAddCampaignGroup() throws Exception {  
  82.   MockHttpServletRequest request = new MockHttpServletRequest();  
  83.   MockHttpServletResponse response = new MockHttpServletResponse();  
  84.   String requestURI = "/campaignGroup/prevAdd";  
  85.   request.setRequestURI(requestURI);  
  86.   request.setMethod("POST");  
  87.   final ModelAndView mav = this.excuteAction(request, response);  
  88.   Assert.assertEquals("createCampaignGroup", mav.getViewName());  
  89.   
  90.  }  
  91.   
  92.  @Test  
  93.  public void prevAddCampaignGroup2() throws Exception {  
  94.   CampaignGroupController userController = context.getBean(CampaignGroupController.class);  
  95.   MockMvc mockMvc = MockMvcBuilders.standaloneSetup(userController)  
  96.     .build();  
  97.   ResultActions perform = mockMvc.perform(MockMvcRequestBuilders.post("/campaignGroup/prevAdd"));  
  98.   ResultActions andExpect = perform  
  99.     .andExpect(MockMvcResultMatchers.status().is(200));  
  100.   andExpect  
  101.     .andExpect(  
  102.       MockMvcResultMatchers.view()  
  103.         .name("createCampaignGroup"));  
  104.  }  
  105.   
  106. }  
  107.   
  108.    
  109.   
  110. ----需要配置2个bean,测试Controller需要用到  
  111.   
  112. <bean  
  113.   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
  114.  </bean>  
  115.  <bean  
  116.   class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  117.  </bean>  
  118.   
  119.    
  120.   
  121. 测试Service(Dao类似)  
  122.   
  123. /** 
  124.  * 这种方式 的单元测试,不需要重复指定 资源文件,减少重复。 麻烦的地方在于,需要手动获取bean。 
  125.  *  
  126.  * @author leiwen@fansunion.cn 
  127.  */  
  128. public class FansUnionServiceTest extends JUnitTestBase {  
  129.   
  130.  private static CampaignGroupService campaignGroupService = context  
  131.    .getBean(CampaignGroupService.class);  
  132.   
  133.    
  134.   
  135.  // 测试add,remove,update方法的时候,已经测试了find?  
  136.  @Test  
  137.  public void addCampaignGroup() {  
  138.   CampaignGroup cg = buildOneGroup();  
  139.   
  140.   campaignGroupService.add(cg);  
  141.   
  142.   CampaignGroup dbCampaignGroup = campaignGroupService.find(cg.getId());  
  143.   Assert.assertEquals(cg, dbCampaignGroup);  
  144.   campaignGroupService.delete(cg.getId());  
  145.  }  
  146.   
  147.    
  148.   
  149. 基于注解的测试  
  150.   
  151. /** 
  152.  *  
  153.  * 基于注解的单元测试。 
  154.  *  
  155.  * @author leiwen@fansunion.cn 
  156.  */  
  157. @RunWith(SpringJUnit4ClassRunner.class)  
  158. @ContextConfiguration(locations = { "classpath:spring-mvc-context.xml",  
  159.   "classpath:spring-common-context.xml" })  
  160. public class FansUnionServiceTestWithAnnotation {  
  161.    
  162.  @Autowired  
  163.  private CampaignGroupService campaignGroupService;  
  164.    
  165.  /** 
  166.   * 根据campaignId获取campaignGroup的name集合<br> 
  167.   * 测试场景:<br> 
  168.   *  
  169.   */  
  170.  @Test  
  171.  public void testListCampaignGroupNameByCampaignId(){  
  172.   System.out.println("**********");  
  173.   System.out.println(campaignGroupService.listCampaignGroupNameByCampaignId(1005899));  
  174.  }  
  175.   
  176. }  
  177.   
  178.    
  179. </pre><br>  

参考资料:http://lohasle.iteye.com/blog/1617929

http://jiuyuehe.iteye.com/blog/1882424

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值