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

发表于:2017-12-21 11:16

字体: | 上一篇 | 下一篇 | 我要投稿

 作者: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只有三个版本,分别是
  · JUnit 3.8.x
  · JUnit 4.x (surefire提供串行执行的provider)
  · JUnit 4.7 (surefire提供支持并行执行的provider)
  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] ------------------------------------------------------------------------
  可以看到测试案例通过

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号