package com.fancyleeo.junitest;
public class Calculator {
public double add(double number1, double number2) {
return number1 + number2;
}
public static void main(String[] args) {
System.out.println("hh");
}
}
package com.fancyleeo.test;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.fancyleeo.junitest.Calculator;
public class CalculatorTest {
private int nbErroes=0;
/*
* This is not a junit test case.
*/
public void testAdd() {
Calculator calculator = new Calculator();
double result = calculator.add(10, 50);
if(result!=60){
System.out.println(result);
throw new IllegalStateException("Bad result: " + result);
}
}
public int nbErrorPlus(){
return ++nbErroes;
}
public int getNbErrorPlus(){
return nbErroes;
}
@Test
public void testAdd2() {
Calculator calculator = new Calculator();
double result = calculator.add(10, 50);
assertEquals(60, result, 0);
}
}
CalculatorTest依赖Calculator,所以要先编译Calculator,然后才能编译CalculatorTest,否则会报:
CalculatorTest.java:32: 找不到符号
符号: 类 Calculator
位置: 类 com.fancyleeo.test.CalculatorTest
Calculator calculator = new Calculator();
^
编译和运行:
D:\asdf>javac -d . Calculator.java
D:\asdf>javac -d . CalculatorTest.java
D:\asdf>java org.junit.runner.JUnitCore com.fancyleeo.test.CalculatorTest
JUnit version 4.6
.
Time: 0.006
OK (1 test)