1.安装JDK;
2.新建文件夹命名为junit-example,下载junit-4.12.jar 和 hamcrest-core-1.3.jar 放入junit-example文件夹中;
3.新建Calculator.java. 放入junit-example文件夹中,内容如下:
1 public class Calculator { 2 public int evaluate(String expression) { 3 int sum = 0; 4 for (String summand: expression.split("\\+")) 5 sum += Integer.valueOf(summand); 6 return sum; 7 } 8 }
然后在cmd中执行命令:javac Calculator.java
4.新建CalculatorTest.java. 放入junit-example文件夹中,内容如下:
1 import static org.junit.Assert.assertEquals; 2 import org.junit.Test; 3 public class CalculatorTest { 4 @Test 5 public void evaluatesExpression() { 6 Calculator calculator = new Calculator(); 7 int sum = calculator.evaluate("1+2+3"); 8 assertEquals(6, sum); 9 } 10 }
然后在cmd中执行命令:javac -cp .;junit-4.12.jar CalculatorTest.java
5.在cmd中执行命令:java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
输出结果为:
JUnit version 4.12 . Time: 0.004 OK (1 test)
6.将Calculator.java中的sum += Integer.valueOf(summand);替换为sum -= Integer.valueOf(summand);
然后在cmd中重新执行命令:javac Calculator.java 及 java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
输出结果为:
JUnit version 4.12 .E Time: 0.007 There was 1 failure: 1) evaluatesExpression(CalculatorTest) java.lang.AssertionError: expected: but was: at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotEquals(Assert.java:834) at org.junit.Assert.assertEquals(Assert.java:645) at org.junit.Assert.assertEquals(Assert.java:631) at CalculatorTest.evaluatesExpression(CalculatorTest.java:9) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77) at org.junit.runner.JUnitCore.main(JUnitCore.java:36) FAILURES!!! Tests run: 1, Failures: 1
7.cmd中命令执行全过程如下图:
时间: 2024-10-12 16:38:31