Sunday, 1 March 2015

JUnit Parameterized Test with Enclosed behaviour

In this post We will be discussing that. Recently, I came across a requirement where there are multiple functionalities present as separate methods enclosed in a single class. The class is a kind of Utililty class type and I need to create Test each of its methods with all possible input types to judge the integrity of each of the functionalities of the class. So, what I came up with approach to create JUnit Parameterized Classes of each of the functionalities, but the problem is that, there are too many JUnit Parameterized Classes are getting created as there are lots of util methods in the class. So, I need to find a way to try to consolidate all the Test Cases in a Single class. I decided to create Inner classes for testing (in a Parameterized way) each of the functionalities and enclose all of them in a Outer class. org.junit.experimental.runners.Enclosed provided me the control that I needed. Let's go through the Code:

@RunWith(Enclosed.class)
public class ParameterizedTest{


 
 @BeforeClass
 public static void init()
 {
  System.out.println(" Setup Class Wide Accessible Resources Here");
 }
 
 
 @AfterClass
 public static void release()
 {
  System.out.println(" Release Class Wide Accessible Resources after all the tests are successfully completed");
 }
 
 @RunWith(value=Parameterized.class)
 public static class TestLogon{

  private String numberA;
  private String numberB;
  
  public TestLogon(String numberA, String numberB) {
   
   this.numberA = numberA;
   this.numberB = numberB;
   
  }
  
  
  @Parameterized.Parameters
  public static Iterable data1() {
   
   return Arrays.asList(new Object[][] { 
    { null, "asd|asd|||ASD||ASD"}
    
   });
  }
        @Test
        public void testLogonRequest1() throws Exception {
         System.out.println(numberA+":"+numberB);
        }
        
        @Test
        public void testLogonRequest2() throws Exception {
         System.out.println("Dummy Tests method to test @Before and @After annotated methods");
        }
        
        @Before
        public void initTestLogon()
        {
         System.out.println("Intialize Configuration which is to be done before each test methods");
        }
        
        @After
        public void releaseTestLogon()
        {
         System.out.println("Release Configuration which is to be done after each test methods");
        }
        
    }

 
 @RunWith(value=Parameterized.class)
 public static class TestLogon2{

  private String numberA;
  private String numberB;
  
  public TestLogon2(String numberA, String numberB) {
   
   this.numberA = numberA;
   this.numberB = numberB;
   
  }
  
  
  @Parameterized.Parameters
  public static Iterable data2() {
   
   return Arrays.asList(new Object[][] { 
    { "1", "asd111|asd99|||ASD990||ASD9897"}
    
   });
  }
        @Test
        public void testLogonRequest() throws Exception {
         System.out.println(numberA+":"+numberB);
        }
    }

 
 @RunWith(value=Parameterized.class)
 public static class TestLogon3{

  private String numberA;
  private String numberB;
  
  public TestLogon3(String numberA, String numberB) {
   
   this.numberA = numberA;
   this.numberB = numberB;
   
  }
  
  
  @Parameterized.Parameters
  public static Iterable data2() {
   
   return Arrays.asList(new Object[][] { 
    { "999", "asd111|asd99|||ASD990||ASD9897"}
    
   });
  }
        @Test
        public void testLogonRequest() throws Exception {
         System.out.println(numberA+":"+numberB);
        }
    }

 
 
}

The output is:

 Setup Class Wide Accessible Resources Here
Intialize Configuration which is to be done before each test methods
null:asd|asd|||ASD||ASD
Release Configuration which is to be done after each test methods
Intialize Configuration which is to be done before each test methods
Dummy Tests method to test @Before and @After annotated methods
Release Configuration which is to be done after each test methods
1:asd111|asd99|||ASD990||ASD9897
999:asd111|asd99|||ASD990||ASD9897
 Release Class Wide Accessible Resources after all the tests are successfully completed

From the output it is quite apparent, the order of execution of the code and the various lifecycle methods of JUnit. Please note that @Before and @After annotated methods we have used with TestLogon, but it could also be implemented in other Inner Classes too. I am not describing how the Parameterized Test Cases work for JUnit as there are lots of good descriptive resources available. Hope this post provides you some help. Any suggestions or opinions are always welcome. Please share your views. Keep Coding ang Keep sharing.....
View Subhankar Paul's profile on LinkedIn

1 comment: