junit4.x
(1), when using the junit4.x version of unit testing, do not have to test the class to inherit the TestCase parent class, because, junit4.x fully introduced annotation to perform the tests we write.
(2), junit4.x version, quoting the way of annotations, unit testing;
(3), junit4.x version of our commonly used annotations:
A, @Before annotations: As with the setup () method function in junit3.x, execute before each test method;
B, @After annotations: As with the teardown () method function in junit3.x, execute after each test method;
C, @BeforeClass Annotations: Execute before all methods are executed;
D, @AfterClass annotations: Executed after all methods have been executed;
E, @Test (timeout = xxx) Note: Set the current test method to run within a certain time, or return an error;
F, @Test (expected = exception.class) Note: Sets whether the method being tested throws an exception. The exception type thrown is: Exception.class;
G, @Ignore annotations: Comment out a test method or a class, the annotated method or class, will not be executed.
Package test;
Import Java.util.Arrays;
Import java.util.Collection;
Import Org.junit.After;
Import Org.junit.AfterClass;
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.BeforeClass;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.junit.runners.Parameterized;
Import Org.junit.runners.Parameterized.Parameters;
(1) Step one: The test class specifies a special runner org.junit.runners.Parameterized
@RunWith (Parameterized.class)
public class TestT1 {
T1 t1;
//(2) Step two: Declare several variables for the test class, respectively, to hold the expected value and the data used for the test.
int x, y, z,expected;
//(3) Step three: Declare a public constructor with parameters for the test class and assign values to several variables declared in the second link.
public TestT1 (int x, int y, int z,int expected) {
System.out.println ("Initialize");
this.x = x;
this.y = y;
this.z = z;
this.expected = expected;
}
//(4) Step four: Declare for the test class A org.junit.runners.Parameterized.Parameters decorated with annotations, with a return value of
///Java.util.Collection public static method, and in this method initializes all parameter pairs that need to be tested.
@Parameters
public static Collection Data () {
}
@BeforeClass
//execute before all methods are executed
public static void Globalinit () {
//system.out.println ("init all Method ...");
}
@AfterClass
//Execute after all methods have been executed
public static void Globaldestory () {
//system.out.println ("Destory all Method ...");
}
@Before
//execute before each test method
public void setUp () {
//system.out.println ("Start SetUp method");
t1=new T1 ();
}
@After
//executed after each test method
public void TearDown () {
//system.out.println ("End Method");
}
@Test
public void TestWork1 () {
Int act = t1.work (x, y, z);
assert.assertequals (expected, ACT);
}
@Test
public void TestWork2 () {
Int act = t1.work (x, y, z);
assert.assertequals (expected, ACT);
}
@Test
public void TestWork3 () {
Int act = t1.work (x, y, z);
assert.assertequals (expected, ACT);
}
@Test (timeout = 1000)
public void TestWork4 () {
Int act = t1.work (x, y, z);
assert.assertequals (expected, ACT);
}
}
A preliminary understanding of JUnit