Java Unit testing using JUnit @RunWith(Suite.class)

As developers we should start writing unit test cases, to be sure the unit of codes we have written works fine. Along with unit testing we can write functional test cases to be ensure our functionality works as expected. In this blog post i'm planing to explain how i used JUnit and selenium for an external OAuth login module.
In my module I needed to support set of external account from different OAuth providers, such as google, facebook, microsoft etc ... This module is responsible for provide a valid access token for different provider from an API exposed to external systems.
There are 3 steps in this module
  1. Add External Account.
  2. Get Tokens.
  3. Delete External Accounts.
I wanted my unit and functional testing to run in the above mentioned sequence. So I used JUnit Suit API, which executed my test classes in given sequence. To order the test methods I used FixMethodOrder API (This is available from JUnit 4.11). I used gradle as the build system.
I'm using 3 classes by the name AddAccountTest, GetTokensTest and DeleteAccountTest.


AddAccountTest.java, GetTokens.java and DeleteAccount.java

 @FixMethodOrder(MethodSorters.NAME_ASCENDING)  
 public class AddAccountTest {  
 //Write your test cases in alphabetical order.  
 }

Using TestSuitsOrder.java to specify JUnit the order of classes to be executed.

 @RunWith(Suite.class)  
 @Suite.SuiteClasses({  
     AddAccountTest.class,  
     GetTokensTest.class,  
     DeleteAccountTest.class  
 })  
 public class TestSuitsOrder {  
 }  

TestExecutionListner.java to be notified events that occurred during the test run

 public class TestExecutionListener extends RunListener  
 {  
   /**  
    * Called before any tests have been run.  
    * */  
   public void testRunStarted(Description description)     throws java.lang.Exception {  
   }  
   /**  
    * Called when all tests have finished  
    * */  
   public void testRunFinished(Result result) throws java.lang.Exception {  
   }  
   /**  
    * Called when an atomic test is about to be started.  
    * */  
   public void testStarted(Description description) throws java.lang.Exception {  
   }  
   /**  
    * Called when an atomic test has finished, whether the test succeeds or fails.  
    * */  
   public void testFinished(Description description) throws java.lang.Exception {  
   }  
   /**  
    * Called when an atomic test fails.  
    * */  
   public void testFailure(Failure failure) throws java.lang.Exception  
   {  
     Throwable exception = failure.getException();  
     exception.printStackTrace();  
   }  
   /**  
    * Called when a test will not be run, generally because a test method is annotated with Ignore.  
    * */  
   public void testIgnored(Description description) throws java.lang.Exception {  
   }  
 }  

TestSuitRunner.java to be executed the test cases.
 public class TestSuitsRunner {  
   public static void main(String[] args)  
   {  
     JUnitCore runner = new JUnitCore();  
     //Adding listener here  
     runner.addListener(new TestExecutionListener());  
     runner.run(TestSuitsOrder.class);  
   }  
 }  

Most importantly you need to exclude the 3 test classes and include TestSuitsRunner.java in build.gradle(Excluding is required to avoid execution of test class multiple times).



 test {  
   exclude '**/AddAccountTest.class'  
   exclude '**/DeleteAccountTest.class'  
   exclude '**/GetTokensTest.class'  
 }  
 task TestSuitsRunner(type: Test, dependsOn: testClasses) {  
   include 'TestSuitsRunner'  
 }  
 tasks.withType(Test) {  
   testLogging {  
     events 'started', 'passed', "failed"  
   }  
 }  

Use ./gradlew test to execute all the test cases.

Comments

Popular posts from this blog

SSL Pinning in Android

Android Event Bus Implementations using Otto