代码样例:
2个类
public class calculator {
int add(int x,int y){
return x+y;
}
int subtract(int x,int y){
return x-y;
}
int multiply(int x,int y){
return x*y;
}
int division(int x,int y){
return x/y;
}
}
import org.junit.Assert;
import org.junit.Test;
public class test {
@Test
public void testadd(){
calculator yf=new calculator();
int result =yf.add(1,2);
Assert.assertEquals(3,result);
System.out.println("...........................");
}
}
因为确实等于3,代码就正常输出,如下
如果不等于了
import org.junit.Assert;
import org.junit.Test;
public class test {
@Test
public void testadd(){
calculator yf=new calculator();
int result =yf.add(1,2);
Assert.assertEquals(2,result);//修改在这
System.out.println("...........................");
}
}
结果如下
编译器就会自动报错
代码样例
只对test进行更改
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class test {
@Before
public void before(){
System.out.println("这里是before");
}
@After
public void after(){
System.out.println("after");
}
@Test
public void testadd(){
calculator yf=new calculator();
int result =yf.add(1,2);
Assert.assertEquals(2,result);
System.out.println("...........................");
}
}
这里不发看出after的执行顺序
我们将代码改成
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class test {
@Before
public void before(){
System.out.println("这里是before");
}
@After
public void after(){
System.out.println("after");
}
@Test
public void testadd(){
calculator yf=new calculator();
int result =yf.add(1,2);
Assert.assertEquals(3,result);
System.out.println("...........................");
}
}
这就显而易见了