Thursday 28 February 2013

jUnit4 new features (Parametric Testing, Data point Testing and @Rule)



PARAMETRIC TESTING:
 @RunWith(value = Parameterized.class)
 public class SampleClassTest {
 int x;
 int y;
 public SampleClassTest(int x, int y) {
 this.x = x;
 this.y = y;
 }
 @Parameters
 public static Collection data(){
 return Arrays.asList( new Object[][] {
 { 1,2 },
 { 8,9 },
 { 6,5 },
 { 4,4 },
 { 2,3 },
 });
 }
 @Test
 public void test() {
 SampleClass sample = new SampleClass();
 int result = sample.add(x, y);
 System.out.println("New result : "+result);
 } }
OUTPUT:
New result :3
New result :17
New result :11
New result :8
New result :5

DATA-POINTS AND THEORIES
@RunWith(Theories.class)
public class JuintTheories {
@DataPoint public static int num3 = 3;
@DataPoint public static int num4 = 6;
@Theory
public void test(int num3,int num4) {
SampleClass sample = new SampleClass();
sample.add(num3,num4);
}
}
OUTPUT:
6
9
9
12
@RULE ANNOTATION
· Use instead of @Before and @After annotations.
· Particularly usefull in writing integration test, where needs same code to be run number of times.
· The purpose of @Rule annotation is to mark up public field of test class.
· Notification on test.
· Special check performed after each test, posibily causing failure of test.
· Making information about the test available inside the test.
Execution flow:
1. Corresponding to each rule constructor of TestRule implemented class are invoked.
2. Test class constructor invokes.
3. TestRule implemented methode ‘Statement apply(Statement,Description)’ of the first rule object is invoked.
4. Create new statement object, then next rule 3 and 4 the step until al rule are coered.
5. Evaluate method of each rule, where all instruction before and test method then after instructioins are executed.

No comments:

Post a Comment