在Maven项目中使用JUnit进行单元测试

2017-06-07  本文已影响437人  beanlam

布置JUnit环境

对于需要使用JUnit进行单元测试的maven项目,添加对JUnit相关jar包的依赖,打开这个project的pom.xml文件,引入以下dependency

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.9</version>
    <scope>test</scope>
</dependency>  

具体要引用哪个版本的JUnit,在version节点配置,scope节点配置一般情况是test,二般情况下也配置的是test,我们引用JUnit只是为了在测试阶段去使用,软件的运行其实是不需要依赖JUnit的,软件的打包部署发布,也不需要将JUnit相关的库带上。

maven-surefire-plugin

在surefire的眼中,它看到的JUnit只有三个版本,分别是

surefire使用哪种provider,要看项目中依赖的JUnit的项目,以及pom.xml中对surefire插件的配置,比如说是否配置了并行执行(parellel),选择的策略有以下伪代码演示:

if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value
    use junit47 provider
if JUnit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1

一般为了避免不必要的奇奇怪怪的麻烦,都直接指定surefire的provider,前提是要提前知道使用的JUnit版本

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.19.1</version>
      </dependency>
    </dependencies>
  </plugin>

进行测试

首先编写一个待测试的类

package io.beansoft.junit.practice;
/**
 *
 *
 * @author beanlam
 * @date 2016年12月8日 下午4:19:19
 * @version 1.0
 *
 */
public class Calculator {
    public int evaluate(String expression) {
        int sum = 0;
        for (String summand : expression.split("\\+"))
            sum += Integer.valueOf(summand);
        return sum;
    }
}  

然后在src/test/java下编写测试类

package io.beansoft.junit.practice;
import org.junit.Assert;
import org.junit.Test;
/**
 *
 *
 * @author beanlam
 * @date 2016年12月9日 下午12:02:11
 * @version 1.0
 *
 */
public class CalculatorTest {
    
    @Test
    public void evaluatesExpression() {
        Calculator calculator = new Calculator();
        int sum = calculator.evaluate("1+2+3");
        Assert.assertEquals(6, sum);
    }
}

邮件点击项目,选择run as,选择maven test,运行结果如下所示

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-practice 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-practice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\eclipse-neon\workspace\junit-practice\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-practice ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-practice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\eclipse-neon\workspace\junit-practice\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-practice ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ junit-practice ---
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.beansoft.junit.practice.CalculatorTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in io.beansoft.junit.practice.CalculatorTest
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.066 s
[INFO] Finished at: 2016-12-09T12:08:06+08:00
[INFO] Final Memory: 10M/219M
[INFO] ------------------------------------------------------------------------  

可以看到测试案例通过

上一篇下一篇

猜你喜欢

热点阅读