Code sample:
2 classes
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("...........................");
}
}
Because it is indeed equal to 3, the code is output normally, as follows.
If it is not equal to
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("...........................");
}
}
The result is as follows, the
compiler will automatically report an error
Code sample
only make changes to 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("...........................");
}
}
We don’t see the execution order of after here.
Let’s change the code to
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("...........................");
}
}
This is obvious