Junit4 Junit快速入门
精华
小牛编辑
194浏览
2023-03-14
在本文中,我们将学习如何编写单元测试。我们将创建一个简单的maven项目,以演示如何创建JUnit测试用例。
我们不使用任何IDE来创建一个maven项目,而是使用命令行来创建一个简单的maven项目。
1 创建简单的Maven项目
让我们通过执行以下命令来创建一个简单的maven项目:
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application
在maven项目构建成功之后,maven将创建一个默认的文件夹结构。
2 项目包装结构
src
- src/main/java:此文件夹包含Java源代码包和类
- src/main/resources:此文件夹包含非Java资源,例如属性文件和Spring配置
test
- src/test/java :此文件夹包含测试源代码包和类
- src/test/resources :此文件夹包含非Java资源,例如属性文件和Spring配置
── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── xnip
│ │ └── junit
│ │ └── Calculator.java
│ ├── resources
└── test
├── java
│ └── com
│ └── xnip
│ └── junit
│ └── CalculatorTest.java
└── resources
3 更新pom.xml文件中的JUnit依赖关系
让我们将下面的JUnit依赖项添加到我们的maven项目中。打开pom.xml文件,并在“依赖项”部分中添加以下依赖项:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
4 创建Calculator.java类
让我们用一些逻辑创建一个Calculator类,以便我们使用JUnit框架对其进行单元测试:
/**
* 小牛知识库网: https://www.xnip.cn
*/
public class Calculator {
public int evaluate(String expression) {
int sum = 0;
for (String summand : expression.split("\\+"))
sum += Integer.valueOf(summand);
return sum;
}
}
4.1 为计算器类创建JUnit测试
创建一个简单的测试用例需要两个步骤。
- 创建方法并使用@ org.junit.Test注释方法
- 当您要检查相等性时,请静态导入org.junit.Assert。*,调用assertEquals(),并传递期望值和实际值。
这是完整的代码:
/**
* 小牛知识库网: https://www.xnip.cn
*/
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}
5 运行测试
使用以下命令行通过以下maven命令运行JUnit测试:
mvn test
输出:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-getting-started 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-getting-started ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-getting-started ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-getting-started\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-getting-started ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-getting-started ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-getting-started\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit-getting-started ---
[INFO] Surefire report directory: E:\Git_Work\junit-developers-guide\junit-getting-started\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.developersguide.junit.CalculatorTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.022 s
[INFO] Finished at: 2018-06-27T21:49:29+05:30
[INFO] Final Memory: 11M/28M
[INFO] ------------------------------------------------------------------------
6 结论
在本文中,我们学习了如何通过创建maven项目来编写简单的JUnit测试用例。