This small example shows you how to write a unit test. You need to have a JDK installed and a text editor. (In general it is recommended to use a build tool for building your software and running the tests.)
一个简单的展示你如何去写一个单元测试。你需要安装一个JDK和一个文本编辑器(比较推荐使用一个构建工具,用于构建你的软件和运行的测试)
Preparation 准备工作
Create a new folder junit-example and download the current junit-4.XX.jar from JUnit’s release page and Hamcrest to this folder.
创建一个文件 junit-example 并且下载 junit相关的jar包刚在该目录下,下载地址
Change to the folder junit-example. All files are created within this folder and all commands are executed there, too.
当改变junit-example文件时,这个文件夹里所有创建的文件和所有命令也都会被执行。
Create the class under test 创建一个类,用来测试
Create a new file Calculator.java and copy the following code to this file.
创建一个文件 Calculator.java,并且拷贝如下代码到该文件中
public class Calculator {
public int evaluate(String expression) {
int sum = 0;
for (String summand: expression.split("\\+"))
sum += Integer.valueOf(summand);
return sum;
}
}
Now compile this class: 编译这个类文件
javac Calculator.java
The Java compiler creates a file Calculator.class.
这个java 编辑器创建了一个文件 Calculator.class.
Create a test 创建一个测试
Create the file CalculatorTest.java and copy the following code to this file.
创建一个文件 CalculatorTest.java 并且复制下面代码到文件中
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);
}
}
Compile the test. On Linux or MacOS
编译这个test在Linux 或者 MackOS上
javac -cp .:junit-4.XX.jar alculatorTest.java
and on Windows
在windows上
javac -cp .;junit-4.XX.jar alculatorTest.java
The Java compiler creates a file >CalculatorTest.class.
java编译器创建一个文件 CalculatorTest.class.
.
Run the test 运行测试Run the test from the command line. On Linux or MacOS
在 Linux 或者 Macos 环境下,从命令行中运行test
java -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
nd on Windows
在windows环境下
java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
The output is
输出结果
JUnit version 4.12
.
Time: 0,006
OK (1 test)
The single . means that one test has been run and the OK in the last line tells you that your test is successful.
这一个 . 的意思是,这个测试已经被运行,并且成功运行到最后一行,告诉你你的测试时成功的。
Let the test fail 测试失败
Modify Calculator.java in order to get a failing test. Replace the line
修改 Calculator.java 为了获得一个错误的测试,替换这一行
sum += Integer.valueOf(summand);
with 用这个替换
sum -= Integer.valueOf(summand);
and recompile the class.
并且重新编译类文件
javac Calculator.java
Run the test again. On Linux or MacOS
重复运行测试,在linux 或者Macos上
java -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
and on Windows
在windows 上
java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
Now the test fails and the output is
现在测试失败,并且输出
JUnit version 4.12
.E
Time: 0,007
There was 1 failure:
1) evaluatesExpression(CalculatorTest)
java.lang.AssertionError: expected:<6> but was:<-6>
at org.junit.Assert.fail(Assert.java:88)
...
FAILURES!!!
Tests run: 1, Failures: 1
JUnit tells you which test failed (evaluatesExpression(CalculatorTest)) and what went wrong:
Junit 告诉你那个测试失败,evaluatesExpression(CalculatorTest) 和 什么出错了
java.lang.AssertionError: expected:<6> but was:<-6>
意思是,断言出错了,,应该是6,,但是结果却是-6