欢迎来到农夫的博客

半亩方塘一鉴开, 天光云影共徘徊。 问渠哪得清如许? 为有源头活水来!

初步认识JUnit

初步认识JUnit

  目前大多数的基于Java的企业应用软件,肯定少不了单元测试,程序员通过编写单元测试来验证自己程序的有效性;管理者通过持续自动的执行单元测试和分析单元测试覆盖率来确保软件本身的质量。可以说单元测试和集成测试在软件开发整个流程中占有举足轻重的地位。

    单元测试,是指对软件中的最小可测试单元进行检查和验证。单元测试不是为了证明程序是对的,而是证明程序没有错。Java常用的单元测试框架有JUnit,TestNG,还有些MOCK框架,这里我们只来讲述JUnit。

JUnit            

JUnit的两种版本是JUnit 3.8和JUnit 4,前者使用反射,后者使用反射和注解。

例子:
Calculator
 1 package com.shop.web.test;
 2 public class Calculator {
 3     private static double result = 0.0;
 4     public void add(double num) {
 5         result = result + num;
 6     }
 7     public void substract(double num) {
 8         result = result - num;
 9     }
10     public void multiply(double num) {
11         result = result * num;
12     }
13     public void divide(double num) {
14         if (num != 0) {
15             result = result / num;
16         } else {
17             result = result;
18         }
19     }
20     // 清零
21     public void clear() {
22         result = 0;
23     }
24     public double getResult() {
25         return result;
26     }
27 }

 CalculatorTest.java

 1 package com.shop.web.test;
 2 import junit.framework.TestCase;
 3 public class CalculatorTest extends TestCase {
 4     private static Calculator calculator = new Calculator();
 5     
 6     @Override
 7     protected void setUp() throws Exception {
 8         System.out.println("JUnit initialize the fixture state by overriding setup ");
 9         calculator.clear();
10     }
11     @Override
12     protected void tearDown() throws Exception {
13         System.out.println("JUnit clean-up after a test by overriding tearDown ");
14         calculator.clear();
15     }
16     public void testAdd() {
17         System.out.println("add result:" + calculator.getResult());
18         calculator.add(10.1);
19         assertEquals(10.1, calculator.getResult());
20     }
21     public void testSubstract() {
22         System.out.println("substract result:" + calculator.getResult());
23         calculator.add(10.1);
24         calculator.substract(2);
25         assertEquals(8.1, calculator.getResult());
26     }
27     public void testMultiply() {
28         System.out.println("multiply result:" + calculator.getResult());
29         calculator.add(12);
30         calculator.multiply(12);
31         assertEquals(144.0, calculator.getResult());
32     }
33     public void testDivide() {
34         System.out.println("divide result:" + calculator.getResult());
35         calculator.add(12);
36         calculator.divide(12);
37         assertEquals(1.0, calculator.getResult());
38     }
39 }

 绿条代表程序没有错误

 

使用JUnit3.8          
需要满足如下原则:
1、测试方法使用test作为方法名的前缀(JUnit设计 用反射找到以test开头的方法)
2、测试方法必须使用public void 进行修饰,不能带任何的参数
3、测试类和目标源代码的类应该位于同一个包下面,即包名应该一致
4、新建一个源代码目录来存放我们的测试代码,即源代码和测试代码分开
5、测试方法中间必须可以独立测试,测试方法之间不能有任何的依赖关系
 
测试类必须继承TestCase:
下面是TestCase的文档说明,junit.framework.TestCase
1 public abstract class TestCase extends Assert implements Test
2         A test case defines the fixture to run multiple tests.
3         To define a test case
4         1) implement a subclass of TestCase
5         2) define instance variables that store the state of the fixture
6         3) initialize the fixture state by overriding setUp
7         4) clean-up after a test by overriding tearDown.
8         Each test runs in its own fixture so there can be no side effects among test runs
1、测试类通过重写 TestCase的setUp()方法,用来初始化测试方法,即在每一个测试方法执行之前执行;
2、测试类通过重写 TestCase的tearDown()方法,用来清理测试方法产生的垃圾,即在每一个测试方法执行之后执行;
如:执行例子计算器test
 1 JUnit initialize the fixture state by overriding setup 
 2 add result:0.0
 3 JUnit clean-up after a test by overriding tearDown 
 4 JUnit initialize the fixture state by overriding setup 
 5 substract result:0.0
 6 JUnit clean-up after a test by overriding tearDown 
 7 JUnit initialize the fixture state by overriding setup 
 8 multiply result:0.0
 9 JUnit clean-up after a test by overriding tearDown 
10 JUnit initialize the fixture state by overriding setup 
11 divide result:0.0
12 JUnit clean-up after a test by overriding tearDown 

 

使用JUnit4.1            
需要满足如下原则:
1、@Test
与JUnit 3.8.1不同,在JUnit 4.1中不再强制要求方法名以test开头,而是允许随意命名,前提是没有继承TestCase,否则注解就不起作用了,其次只要符合Java的命名规范就行,但测试用例必须以@Test注解。
多个@Test没有明确的执行顺序,4.11版本MethodSorters可以定义执行顺序,如果你想要改变test的执行顺序,那么你可以简单的在test类上加以个注解@FixMethodOrder 并且指定一个合适的MethodSorters
  @FixMethodOrder(MethodSorters.JVM) : 根据JVM返回的顺序来决定test方法的执行顺序。每次测试这个顺序可能都不一样
  @FixMethodOrder(MethodSorters.NAME_ASCENDING) : 根据test方法名按照字典顺序升序排序
2、@Before和@After
JUnit3.8.1中的setUp和tearDown这两个方法,分别使用@Before和@After来进行注解,前者在每个测试方法开始之前执行,多用来做初始化;后者在每个测试方法完成之后执行,多用来清理资源。 注解必须用在public方法。
3、@BeforeClass和@AfterClass
JUnit 4.1还提供了@BeforeClass和@AfterClass注解,功能与@Before和@After类似,但前者是用在所有用例执行之前做初始化、之后做清理,而后者是在每个用例执行之前做初始化、之后做清理。注解必须用在public static方法
注意:前提是没有继承TestCase,否则注解就不起作用了,就必须使用test开头的方法。
 1 public class CalculatorTest4 {
 2     private static Calculator calculator = new Calculator();
 3     
 4     @Before
 5     public void setUp() throws Exception {
 6         System.out.println("JUnit initialize the fixture state by overriding setup ");
 7         calculator.clear();
 8     }
 9     @After
10     public void tearDown() throws Exception {
11         System.out.println("JUnit clean-up after a test by overriding tearDown ");
12         calculator.clear();
13     }
14     @Test
15     public void add() {
16         System.out.println("add result:" + calculator.getResult());
17         calculator.add(10.1);
18         assertEquals(10.1, calculator.getResult());
19     }

 4、@Test(expected=*.class)
在JUnit4.0之前,对错误的测试,我们只能通过fail来产生一个错误,并在try块里面assertTrue(true)来测试。现在,通过@Test元数据中的expected属性。expected属性的值是一个异常的类型,用来检查抛出预期异常。

1 @Test(expected=ArithmeticException.class)
2     public void divide(){
3         int i = 2/0;
4     }

5、@Test(timeout=xxx):
该元数据传入了一个时间(毫秒)给测试方法,
如果测试方法在制定的时间之内没有运行完,则测试也失败。

@Test(timeout=1)
    public void count(){
        for (int i = 0; i < 1000000000; i++) {
            System.out.println(i);
        }
    }

 

6、@ignore:
该元数据标记的测试方法在测试中会被忽略。当测试的方法还没有实现,或者测试的方法已经过时,或者在某种条件下才能测试该方法(比如需要一个数据库联接, 而在本地测试的时候,数据库并没有连接),那么使用该标签来标示这个方法。同时,你可以为该标签传递一个String的参数,来表明为什么会忽略这个测试 方法。比如:@lgnore(“该方法还没有实现”),在执行的时候,仅会报告该方法没有实现,而不会运行测试方法。
1 @Ignore("此方法现在不需要")
2 @Test
3 public void ignore(){
4 System.out.println("不需要");
5 }

 

Spring整合JUnit          

 Junit测试Spring可以很方便的进行。 

用到jar包:spring-test-xxx.jar,junit4的jar。

需要注解 @RunWith、@ContextConfiguration

  @RunWith如:   @RunWith(SpringJUnit4ClassRunner.class)   //表示继承了SpringJUnit4ClassRunner

  @ContextConfiguration如: 用来加载Spring配置文件,@ContextConfiguration(locations = {"classpath:applicationContext-mybatis.xml",……"})

注意:(1)如果spring配置文件applicationContext.xml在classpath路径下,即通常的src目录下,这样加载配置文件,用classpath前缀。

     (2)但是在web项目中,有些人喜欢把spring配置文件applicationContext.xml放在WEB-INF目录下,这里不是classpath目录。这种情况可以按如下方式配置:用file前缀,指定配置文件的绝对路径。貌似这种方式不是很友好。 如:locations = { "file:D:\\workspace\\webproxy\\src\\main\\resources\\" + "applicationContext.xml" }

完整代码如下:

package com.shop.web.test;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.shop.web.entity.ShopUser;
import com.shop.web.service.ShopUserService;
import com.shop.web.util.DateUtil;
/**
 * service、dao层的测试类
 * @author ces
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)//表示继承了SpringJUnit4ClassRunner类 
@ContextConfiguration(locations = {"classpath:applicationContext-mybatis.xml","classpath:applicationContext-service.xml","classpath:applicationContext-transaction.xml"})
public class ShopControllerTest {
    
    private static Logger logger = Logger.getLogger(ShopControllerTest.class);
    
    @Resource
    private ShopUserService shopUserService;
    
    @Transactional
    @Test
    public void getShopUserById(){
        ShopUser shopUser = new ShopUser();
        shopUser.setUserid(6);
        shopUser.setUsername("zhangsan");
        shopUser.setPassword("333");
        shopUser.setCreateTime(Long.parseLong(DateUtil.getString(new Date(), DateUtil.YMDHMS)));
        try {
            shopUserService.insertSelective(shopUser);
            int i = 2/0;
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("ShopControllerTest" + e);
        }
        
        logger.info(JSON.toJSONString("*********"));
    }
}

 

 

 

 

 

 

posted on 2016-08-21 00:07  allTime  阅读(294)  评论(0编辑  收藏  举报

导航