J2EE 第二阶段项目之JUnit4进行单元测试(五)

 今天学习了JUnit4进行单元测试。这样就可以不写页面直接进行过功能模块测试。也不是很深入的了解。

JUnit4和自己写的代码可以分割开来。

 首先呢准备两个jar包:

   

 可以对mapper进行测试,当然也可以对service进行测试。

 

  

 1 package com.lovo.test;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 import com.lovo.entitys.Teach;
10 import com.lovo.service.ITeachService;
11 
12 // 这里的注释很重要的
13 @RunWith(SpringJUnit4ClassRunner.class)
14 @ContextConfiguration(locations={   // 这里写路径,如果有多个路径,用逗号隔开
15         "classpath:applicationContext.xml"    
16 })
17 public class TeachServiceTest {
18     @Resource
19     private ITeachService teachService;
20        
21      @Test  // 使用该注解的方法为测试方法,自动运行时能够识别并被执行
22       public void findTeachById(){
23          try {
24         Teach t= teachService.findTeachById(1);
25         System.out.println(t);
26         } catch (Exception e) {
27             e.printStackTrace();
28         }
29            
30       }
31 }

    出错的情况

 

  在文本文档中Ctrl+v 显示出错信息 如

  

  找出错误信息: 就是找不到 applicationContext.xml文件。

  正确路径:

   

"classpath:xmls/applicationContext.xml"    

正确结果:

 

今天主要是把数据显示出来了。接下来就是增 修改 查询了。

明天就开始         :          2 地区类别统计

                   1 分类别统计

                        1 申报时   (查)

                        2 审批后 (查)         

                   2 分地区统计 

                        1 申报时   (查)

                        2 审批后   (查)

原文地址:https://www.cnblogs.com/hellokitty1/p/5259638.html