Quantcast
Channel: WATER TALKS
Viewing all articles
Browse latest Browse all 19
тЖз

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

$
0
0


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.
тЖз

Viewing all articles
Browse latest Browse all 19

Latest Images

Trending Articles



Latest Images