在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

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

2016-12-20 13:11| 发布者: zhangjf| 查看: 434| 评论: 0

摘要: 布置JUnit环境 对于需要使用JUnit进行单元测试的maven项目,添加对JUnit相关jar包的依赖,打开这个project的pom.xml文件,引入以下dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId ...
布置JUnit环境

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

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.9</version>
  5. <scope>test</scope>
  6. </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),选择的策略有以下伪代码演示:

  1. if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value
  2. use junit47 provider
  3. if JUnit >= 4.0 is present
  4. use junit4 provider
  5. else
  6. use junit3.8.1
复制代码

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

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <version>2.19.1</version>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.apache.maven.surefire</groupId>
  8. <artifactId>surefire-junit47</artifactId>
  9. <version>2.19.1</version>
  10. </dependency>
  11. </dependencies>
  12. </plugin>
复制代码
进行测试

首先编写一个待测试的类

  1. package io.beansoft.junit.practice;
  2. /**
  3. *
  4. *
  5. * @author beanlam
  6. * @date 2016年12月8日 下午4:19:19
  7. * @version 1.0
  8. *
  9. */
  10. public class Calculator {
  11. public int evaluate(String expression) {
  12. int sum = 0;
  13. for (String summand : expression.split("\+"))
  14. sum += Integer.valueOf(summand);
  15. return sum;
  16. }
  17. }
复制代码

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

  1. package io.beansoft.junit.practice;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. /**
  5. *
  6. *
  7. * @author beanlam
  8. * @date 2016年12月9日 下午12:02:11
  9. * @version 1.0
  10. *
  11. */
  12. public class CalculatorTest {
  13. @Test
  14. public void evaluatesExpression() {
  15. Calculator calculator = new Calculator();
  16. int sum = calculator.evaluate("1+2+3");
  17. Assert.assertEquals(6, sum);
  18. }
  19. }
复制代码

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

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Building junit-practice 0.0.1-SNAPSHOT
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO]
  7. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-practice ---
  8. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  9. [INFO] skip non existing resourceDirectory E:eclipse-neonworkspacejunit-practicesrcmainresources
  10. [INFO]
  11. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-practice ---
  12. [INFO] Nothing to compile - all classes are up to date
  13. [INFO]
  14. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-practice ---
  15. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  16. [INFO] skip non existing resourceDirectory E:eclipse-neonworkspacejunit-practicesrctestresources
  17. [INFO]
  18. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-practice ---
  19. [INFO] Nothing to compile - all classes are up to date
  20. [INFO]
  21. [INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ junit-practice ---
  22. -------------------------------------------------------
  23. T E S T S
  24. -------------------------------------------------------
  25. Running io.beansoft.junit.practice.CalculatorTest
  26. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in io.beansoft.junit.practice.CalculatorTest
  27. Results :
  28. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  29. [INFO] ------------------------------------------------------------------------
  30. [INFO] BUILD SUCCESS
  31. [INFO] ------------------------------------------------------------------------
  32. [INFO] Total time: 2.066 s
  33. [INFO] Finished at: 2016-12-09T12:08:06+08:00
  34. [INFO] Final Memory: 10M/219M
  35. [INFO] ------------------------------------------------------------------------
复制代码

可以看到测试案例通过

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-7 21:59

Copyright 2015-2025 djqfx

返回顶部