Friday, 6 July 2018

Junit: @ClassRule annotation

It is like @Rule annotation, only difference is, this rule only applicable to static fields, methods.


For example, below example calculates the execution time of all the test cases.


TestCaseExecutionTimer.java
package com.sample.customrules;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class TestCaseExecutionTimer implements TestRule{
 @Override
 public Statement apply(Statement base, Description description) {
  return new Statement() {
   @Override
   public void evaluate() throws Throwable {
    long time1 = System.currentTimeMillis();
    base.evaluate();
    long time2 = System.currentTimeMillis();
    
    System.out.println("Test cases in " + description.getClassName() + " takes " + (time2-time1) + " milliseconds");
   }
  };
 }
}

TestApp.java
package com.sample.test;

import org.junit.ClassRule;
import org.junit.Test;

import com.sample.customrules.TestCaseExecutionTimer;

public class TestApp {
 @ClassRule
 public static TestCaseExecutionTimer timer = new TestCaseExecutionTimer();

 @Test
 public void testCase1() throws InterruptedException {
  System.out.println("Executing testCase1");
  Thread.sleep(500);
 }

 @Test
 public void testCase2() throws InterruptedException {
  System.out.println("Executing testCase2");
  Thread.sleep(100);
 }

}


When you ran TestApp.java, you can able to see below messages in console.

Executing testCase1
Executing testCase2
Test cases in com.sample.test.TestApp takes 609 milliseconds

Now let’s combine both @Rule and @ClassRule to get each test case execution time and cumulative execution time.

TestCaseExecutionTimer.java

package com.sample.customrules;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class TestCaseExecutionTimer implements TestRule {
 @Override
 public Statement apply(Statement base, Description description) {
  return new Statement() {
   @Override
   public void evaluate() throws Throwable {
    long time1 = System.currentTimeMillis();
    base.evaluate();
    long time2 = System.currentTimeMillis();

    if (description.getMethodName() != null) {
     System.out.println(description.getMethodName() + " takes " + (time2 - time1) + " milliseconds");
    } else {
     System.out.println("Test cases in " + description.getClassName() + " takes " + (time2 - time1)
       + " milliseconds");
    }

   }
  };
 }
}

TestApp.java

package com.sample.test;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import com.sample.customrules.TestCaseExecutionTimer;

public class TestApp {
 @ClassRule
 public static TestCaseExecutionTimer CumulativeTimer = new TestCaseExecutionTimer();

 @Rule
 public TestCaseExecutionTimer testCaseTimer = new TestCaseExecutionTimer();
 
 @Test
 public void testCase1() throws InterruptedException {
  System.out.println("Executing testCase1");
  Thread.sleep(500);
 }

 @Test
 public void testCase2() throws InterruptedException {
  System.out.println("Executing testCase2");
  Thread.sleep(100);
 }

}


When you ran TestApp.java, you can able to see below messages in console.

Executing testCase1
testCase1 takes 501 milliseconds
Executing testCase2
testCase2 takes 100 milliseconds
Test cases in com.sample.test.TestApp takes 606 milliseconds








Previous                                                 Next                                                 Home

No comments:

Post a Comment