JUnit Study Notes
Difference between JUnit3 and JUnit4 Junit3:
The test class must inherit the classTestCase; The test method must start with test; Junit4:
The test class does not need to be inherited.TestCaseThere are no specific requirements for the name of the test method, as long as you add@TestYou can use@beforeSubstitutionsetUpMethod,@AfterSubstitutiontearDownMethod;
In a test class, you can even use multiple@BeforeTo annotate multiple methods. These methods are run before each test. Note,@BeforeIt is initialized once before each test method is run.@AfterIs run once after each test method is completedThat is to say, after the initialization and cancellation of the two annotations, the independence of each test method can be ensured without mutual interference. Its disadvantage is that the efficiency is low.
Added a new feature in JUnit4. Two annotations are added:@BeforeClassAnd@AfterClass, Use the two annotation methods to run each method before and after the test of the method in the test class, instead of running each method.You can use these two annotations for projects with high resource consumption.In addition, Junit4 can perform an exception test (@Test(expected=*.class)) And timeout test (@Test(timeout=xxx)), And provides a new assert method:assertEquals(Object[] expected, Object[] actual)Used to compare array data. JUnit3's use 1 test goal
package club.chuxing.junit4;public class Calculator { public double add(double a, double b) { return a + b; } public double minus(double a, double b) { return a - b; } public double multiply(double a, double b) { return a * b; } public double divide(double a, double b) { return a / b; }}
2 Test Cases
TestCalculator. java:
package club.chuxing.junit4;import junit.framework.TestCase;public class TestCalculator extends TestCase{ private Calculator calc; protected void setUp() throws Exception { super.setUp(); calc = new Calculator(); } protected void tearDown() throws Exception { super.tearDown(); } public void testAdd() { assertEquals(3.0, calc.add(1, 2)); } public void testMinus() { assertEquals(-1.0, calc.minus(1, 2)); } public void testMultiply() { assertEquals(2.0, calc.multiply(1, 2)); } public void testDivide() { assertEquals(0.5, calc.divide(1, 2)); }}
TestCalculator2.java:
package club.chuxing.junit4;import junit.framework.TestCase;public class TestCalculator2 extends TestCase{ private Calculator calc; protected void setUp() throws Exception { super.setUp(); calc = new Calculator(); } protected void tearDown() throws Exception { super.tearDown(); } public void testAdd() { assertEquals(4.0, calc.add(2, 2)); } public void testMinus() { assertEquals(0.0, calc.minus(2, 2)); } public void testMultiply() { assertEquals(4.0, calc.multiply(2, 2)); } public void testDivide() { assertEquals(1.0, calc.divide(2, 2)); }}
3. Test Unit
package club.chuxing.junit4;import junit.framework.Test;import junit.framework.TestSuite;import junit.textui.TestRunner;public class TestAll extends TestSuite { public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(TestCalculator.class); suite.addTestSuite(TestCalculator2.class); return suite; } public static void main(String[] args) { TestRunner.run(suite()); }}
Running result:
........Time: 0.009OK (8 tests)
JUnit4 usage
CalculatorTest. java
package club.chuxing.junit4;import static org.junit.Assert.*;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;public class CalculatorTest { private static Calculator calc; @BeforeClass public static void setUpBeforeClass() throws Exception { calc = new Calculator(); } @AfterClass public static void tearDownAfterClass() throws Exception { } @Test public void testAdd() { assertEquals(3, calc.add(1, 2), 0.00001); } @Test public void testMinus() { assertEquals(3.0, calc.add(1, 2), 0.00001); } @Test public void testMultiply() { assertEquals(3.0, calc.add(1, 2), 0.00001); } @Test public void testDivide() { assertEquals(3.0, calc.add(1, 2), 0.00001); }}
Running result: