【JUnit学习笔记】1:使用JUnit4进行简易单元测试的例子

参考:1,2,3

程序结构

这里写图片描述

JUnit运行配置

在IDEA中:
这里写图片描述
在Eclipse中直接右键,Run AS->JUnit Test即可。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.lzh</groupId>
    <artifactId>testJUnit</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.14.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>
    </dependencies>

</project>

Calculation类

这是要测试的方法所在的类。

package org.lzh;

import org.springframework.stereotype.Component;

//使用@Component注册为bean以使能被扫描注入
@Component
public class Calculation {

    public int add(int a,int b){
        return a+b+1;//故意出错
    }

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

MyJUnit4Test测试类

package org.lzh.test;

import org.junit.*;
import org.junit.runner.RunWith;
import org.lzh.Calculation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

//测试运行器
@RunWith(SpringJUnit4ClassRunner.class)
//配置文件路径
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyJUnit4Test {

    @Autowired
    Calculation calculation;//要测试的类的对象
    int result;//测试结果

    //在JUnit4中使用@Test标注为测试方法
    @Test
    //测试方法必须是public void的
    public void testAdd() {
        System.out.println("---testAdd开始测试---");

        //每个里面只测一次,因为assertEquals一旦测试发现错误就抛出异常,不再运行后续代码
        result = calculation.add(1, 2);
        assertEquals(3, result);

        System.out.println("---testAdd正常运行结束---");
    }

    //又一个测试方法
    //timeout表示测试允许的执行时间毫秒数,expected表示忽略哪些抛出的异常(不会因为该异常导致测试不通过)
    @Test(timeout = 1, expected = NullPointerException.class)
    public void testSub() {
        System.out.println("---testSub开始测试---");

        result = calculation.sub(1, 2);
        assertEquals(-1, result);

        throw new NullPointerException();

        //System.out.println("---testSub正常运行结束---");
    }


    //指示该[静态方法]将在该类的[所有]测试方法执行之[前]执行
    @BeforeClass
    public static void beforeAll() {
        System.out.println("||==BeforeClass==||");
        System.out.println("||==通常在这个方法中加载资源==||");
    }

    //指示该[静态方法]将在该类的[所有]测试方法执行之[后]执行
    @AfterClass
    public static void afterAll() {
        System.out.println("||==AfterClass==||");
        System.out.println("||==通常在这个方法中释放资源==||");
    }

    //该[成员方法]在[每个]测试方法执行之[前]执行
    @Before
    public void beforeEvery() {
        System.out.println("|==Before==|");
    }

    //该[成员方法]在[每个]测试方法执行之[后]执行
    @After
    public void afterEvery() {
        System.out.println("|==After==|");
    }

}

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描bean-->
    <context:component-scan base-package="org.lzh"/>
</beans>

运行结果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/shu15121856/article/details/81334411