【开发工具】Junit4单元测试

Java单元测试工具

增加代码量,减少项目测试时间

断言机制

eclipse自带插件,无需下载

怎么开始使用junit

需要测试的类

package com.imooc.util;

public class Calculate {
    public int add(int a,int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public int multiply(int a,int b) {
        return a * b;
    }

    public int divide(int a ,int b) {
        return a / b;
    }
}

Junit测试类:
快捷方式:在要测试的类上右键 New
选择junit
更改测试类存放的包路径
选择要测试的方法
点击完成

package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class CalculateTest {

    /*
     * 1.测试方法上必须使用@Test进行修饰
     * 2.测试方法必须使用public void 进行修饰,不能带任何的参数
     * 3.新建一个源代码目录来存放我们的测试代码
     * 4.测试类的包应该和被测试类保持一致
     * 5.测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖
     * 6.测试类使用Test作为类名的后缀(不是必须)
     * 7.测试方法使用test作为方法名的前缀(不是必须)
     */
    @Test
    public void testAdd() {
        assertEquals(6, new Calculate().add(3,3));
    }

    @Test
    public void testSubtract() {
        assertEquals(3, new Calculate().subtract(5,2));
    }

    @Test
    public void testMultiply() {
        assertEquals(4, new Calculate().multiply(2, 2));
    }

    @Test
    public void testDivide() {
        assertEquals(3, new Calculate().divide(6, 2));
    }
}

右键run as -> junit test
可以看到junit的视图和输出结果:
Runs:1/1 执行了1个测试方法,1个运行成功
Errors:跑出异常的方法个数
Faliures:测试失败的方法个数

有用的总结:
1. 测试方法上必须使用@Test进行修饰
2. 测试方法必须使用public void 进行修饰,不能带任何的参数
3. 新建一个源代码目录来存放我们的测试代码
4. 测试类的包应该和被测试类保持一致
5. 测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖
6. 测试类使用Test作为类名的后缀(不是必须)
7. 测试方法使用test作为方法名的前缀(不是必须)

注意一点:junit的断言方法不用写成类.方法名的形式,因为junit是静态导入 import static的

测试失败的例子

测试代码

package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class ErrorAndFailureTest {
    /*
     * 1.Failure一般由单元测试使用的断言方法判断失败所引起的,测试点发现了问题
     * ,就是说程序输出的结果和我们预期的不一样。
     * 2.error是由代码异常引起的,它可以产生于测试代码本身的错误,也可以是被测试代码中的
     * 一个隐藏的bug
     * 3.测试用例不是用来证明你是对的,而是用来证明你没有错。
     */
    @Test
    public void testAdd() {
        assertEquals(5, new Calculate().add(3,3));
    }

    @Test
    public void testDivide() {
        assertEquals(3, new Calculate().divide(6, 0));
    }
}

testAdd测试结果Errors为0,Failures为1,因为只是和预期值不一样
testDivide测试结果Errors为1,Failures为0,因为抛出异常了

  1. Failure一般由单元测试使用的断言方法判断失败所引起的,测试点发现了问题,就是说程序输出的结果和我们预期的不一样。
  2. error是由代码异常引起的,它可以产生于测试代码本身的错误,也可以是被测试代码中的一个隐藏的bug
  3. 测试用例不是用来证明你是对的,而是用来证明你没有错。

junit运行流程

一个完整的junit测试类:
右键新建junit测试,勾选junit的所有方法

package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class JunitFlowTest {

    /*
     * 1.@BeforeClass修饰的方法会在所有方法被调用前被执行,
     * 而且该方法是静态的,所以当测试类被加载后接着就会运行它,
     * 而且在内存中它只会存在一份实例,它比较适合加载配置文件。
     * 2.@AfterClass所修饰的方法通常用来对资源的清理,如关闭数据库的连接
     * 3.@Before和@After会在每个测试方法的前后各执行一次。
     * 
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("this is beforeClass...");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("this is afterClass...");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("this is before...");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("this is after");
    }

    @Test
    public void test1() {
        System.out.println("this is test1...");
    }

    @Test
    public void test2(){
        System.out.println("this is test2...");
    }

}

输出结果:
this is beforeClass…
this is before…
this is test1…
this is after
this is before…
this is test2…
this is after
this is afterClass…

  1. @BeforeClass修饰的方法会在所有方法被调用前被执行,而且该方法是静态的,所以当测试类被加载后接着就会运行它,而且在内存中它只会存在一份实例,它比较适合加载配置文件。
  2. @AfterClass所修饰的方法通常用来对资源的清理,如关闭数据库的连接
  3. @Before和@After会在每个测试方法的前后各执行一次。

常用注解

package com.imooc.util;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;

public class AnotationTest {

    /*
     * @Test:将一个普通的方法修饰成为一个测试方法
     *      @Test(expected=XX.class)
     *      @Test(timeout=毫秒 )
     * @BeforeClass:它会在所有的方法运行前被执行,static修饰
     * @AfterClass:它会在所有的方法运行结束后被执行,static修饰
     * @Before:会在每一个测试方法被运行前执行一次
     * @After:会在每一个测试方法运行后被执行一次
     * @Ignore:所修饰的测试方法会被测试运行器忽略
     * @RunWith:可以更改测试运行器 org.junit.runner.Runner
     */
    //测试通过,预期就是抛出一个算术异常
    @Test(expected=ArithmeticException.class)
    public void testDivide() {
        assertEquals(3, new Calculate().divide(6, 0));
    }
    //控制避免发生死循环,时长控制为2000ms,到了2s之后就停止循环,也可以做性能测试
    @Ignore("...")
    @Test(timeout=2000)
    public void testWhile() {
        while(true) {
            System.out.println("run forever...");
        }
    }
    //预期读取文件花3秒,实际测试用了休眠模拟,花了2秒,所以测试成功
    @Test(timeout=3000)
    public void testReadFile(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@Test:将一个普通的方法修饰成为一个测试方法
- @Test(expected=XX.class)
- @Test(timeout=毫秒 )
@BeforeClass:它会在所有的方法运行前被执行,static修饰
@AfterClass:它会在所有的方法运行结束后被执行,static修饰
@Before:会在每一个测试方法被运行前执行一次
@After:会在每一个测试方法运行后被执行一次
@Ignore:所修饰的测试方法会被测试运行器忽略
@RunWith:可以更改测试运行器 org.junit.runner.Runner

测试套件

批量运行测试类

测试套件入口类

package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})
public class SuiteTest {
    /*
     * 1.测试套件就是组织测试类一起运行的
     * 
     * 写一个作为测试套件的入口类,这个类里不包含其他的方法
     * 更改测试运行器Suite.class
     * 将要测试的类作为数组传入到Suite.SuiteClasses({})
     */

}

3个测试类

package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class TaskTest1 {

    @Test
    public void test() {
        System.out.println("this is TaskTest1...");
    }

}
package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class TaskTest2 {

    @Test
    public void test() {
        System.out.println("this is TaskTest2...");
    }

}
package com.imooc.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class TaskTest3 {

    @Test
    public void test() {
        System.out.println("this is TaskTest3...");
    }

}

junit参数设置

对多组数据进行测试

package com.imooc.util;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterTest {
    /*
     * 1.更改默认的测试运行器为RunWith(Parameterized.class)
     * 2.声明变量来存放预期值 和结果值
     * 3.声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰
     * 4.为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
     */
    int expected =0;
    int input1 = 0;
    int input2 = 0;

    @Parameters
    public static Collection<Object[]> t() {
        return Arrays.asList(new Object[][]{
                {3,1,2},
                {4,2,2}
        }) ;
    }

    public ParameterTest(int expected,int input1,int input2) {
        this.expected = expected;
        this.input1 = input1;
        this.input2 = input2;
    }

    @Test
    public void testAdd() {
        assertEquals(expected, new Calculate().add(input1, input2));
    }

}
  1. 更改默认的测试运行器为RunWith(Parameterized.class)
  2. 声明变量来存放预期值 和结果值
  3. 声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰
  4. 为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值

整合测试

spring+hibernate

spring部分:
spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>
    <bean id="date" class="java.util.Date"/>
</beans>

测试类:

package com.imooc.conform;

import java.util.Date;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

    private static ApplicationContext context = null;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void test() {
        Date date =  (Date) context.getBean("date");
        System.out.println(date);
    }

}

hibernate配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/mytest</property>
        <property name="connection.username">root</property>
        <property name="connection.password">mysql</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>

    </session-factory>

</hibernate-configuration>

测试类

package com.imooc.conform;

import org.hibernate.Session;
import org.junit.Test;

public class HibernateTest {

    @Test
    public void test() {
        Session session =  HibernateSessionFactory.getSession();
        System.out.println(session);
    }

}

spring+hibernate整合测试

package com.imooc.conform;

import static org.junit.Assert.*;

import org.hibernate.impl.SessionFactoryImpl;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAndHibernate {

    private static ApplicationContext context;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void test() {
        SessionFactoryImpl bean =  (SessionFactoryImpl) context.getBean("sessionFactory");
        System.out.println(bean);
    }

}

附录:
Junit4 API地址
最近版本4.12
https://junit.org/junit4/javadoc/latest/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值