Thursday, 5 March 2015

JUnit Parameterized Test Cases with Groovy

Lets traverse through the code first:

@RunWith(Enclosed)
 class ParameterizedTest{


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

   def numberA
   def numberB
  
   TestLogon( numA,  numB) {
   
   numberA = numA
   numberB = numB
   
  }
  
  
  
  @Parameters static data1() {
   
   [
    [null, "asd|asd|||ASD||ASD"],
    ['09909', "asd|asd|||ASD||ASD"]
    
   ]*.toArray()
  }
        @Test
        void testLogonRequest1() throws Exception {
         println numberA+":"+numberB
        }
        
        @Test
        void testLogonRequest2() throws Exception {
         println "Dummy Tests method to test @Before and @After annotated methods"
        }
        
        @Before
        void initTestLogon()
        {
         println "Intialize Configuration which is to be done before each test methods"
        }
        
        @After
       void releaseTestLogon()
        {
         println "Release Configuration which is to be done after each test methods"
        }
        
    }

 
 @RunWith(Parameterized)
 static class TestLogon2{

   def numberA
   def numberB
  
   TestLogon2( numA,  numB) {
   
   numberA = numA
   numberB = numB
   
  }
  
  
  
  @Parameters static data2() {
   
   [
    [ "1", "asd111|asd99|||ASD990||ASD9897"]
    
   ]*.toArray()
  }
        @Test
       void testLogonRequest() throws Exception {
         println numberA+":"+numberB
        }
    }

 
 @RunWith(Parameterized)
  static class TestLogon3{

  def numberA
  def numberB
  
   TestLogon3( numA,  numB) {
   
   numberA = numA
   numberB = numB
   
  }
  
  
  
  @Parameters static data2() {
   
   [
    [ "999", "asd111|asd99|||ASD990||ASD9897"]
    
   ]*.toArray()
  }
        @Test
         void testLogonRequest() throws Exception {
         println numberA+":"+numberB
        }
    }

 
 
}

The output of the above code snippet 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
Intialize Configuration which is to be done before each test methods
09909: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

The main thing I want to point out in the above code snippet is the @Parameters annotated data methods. The parameters are all provided in a 2D array format and toArray() is called using spread(*) operator instead of calling toArray() on each of the array and thus avoiding redundancy i.e.
[
    [null, "asd|asd|||ASD||ASD"],
    ['09909', "asd|asd|||ASD||ASD"]
    
   ]*.toArray()

instead of
[
    [null, "asd|asd|||ASD||ASD"].toArray(),
    ['09909', "asd|asd|||ASD||ASD"].toArray()
    
   ]

Interesting right.... Like this there are loads of Groovy features, which are really mindblowing, I will try to share those in a separate post. Please share your views and ideas and Till Next time Keep Coding......
View Subhankar Paul's profile on LinkedIn

No comments:

Post a Comment