Junit Unit test Lab report first, the experimental environment
MyEclipse2014,Junit4.10
TwoExperimental Purpose
Learn unit tests and perform Junit tests in myeclipse
ThreeExperimental Steps1.write the class you want to test
The code is as follows:
Package com.cheng.test;
Public class Calculate {
Public int Add (int A,int b) {
return a+b;
}
Public int Sub (int A,int b) {
return a-2*b; Bug The calculation subtraction should be a-B;
}
Public int divide (int A,int b) {
return A/b;
}
Public int mul (int A,int b) {
return a*b;
}
}
2.add junit4.10.jar
After adding junit4-10 , the file directory is as follows:
3.new test class for testing previously written classes
The directory structure is as follows:
The test class code is as follows:
Package com.cheng.test;
Import static Org.junit.Assert. assertequals;
import Org.junit.Before;
import org.junit.Test;
Public class testcalculate {
Calculate Cal;
@Before
Public void setUp () {
Cal = new Calculate ();
}
@Test
Public void Testadd () {
assertequals ("Additive Problem", 5,cal.add (2,3)); Test addition is correct
}
@Test
Public void testsub () {
assertequals ("Subtraction Problem", 8,cal.sub (10, 2));
}
@Test
Public void Testmul () {
assertequals ("Multiplication Problem", 20,cal.mul (10, 2));
}
@Test
Public void Testdiv () {
assertequals ("Problem with Division", 5,cal.divide (10, 2));
}
}
4.run junittest
Bundle test results to find the previous bug, modify the source code in the SRC folder, run junittest again
Add part Modified code :
Public int Sub (int A,int b) {
return A-B; Before is a-2*b; The calculation subtraction should be a-B;
}
Testing Java code with JUnit