...

Software Testing - Junit

Back to Course

Lesson Description


Lession - #1219 Junit Execution Procedure


The execution procedure is as per the following −

  • First of all, the beforeClass(>
    method executes just a single time.
  • The afterClass(>
    method executes just a single time.
  • The previously(>
    method executes for each test case, but prior to executing the test case.
  • The after(>
    method executes for each test case, but after the execution of test case.
  • In the between before(>
    and after(>
    , each test case executes.

    Let's see an example

    
    
    import org.junit.After;
    import org.junit.AfterClass;
    
    import org.junit.Before;
    import org.junit.BeforeClass;
    
    import org.junit.Ignore;
    import org.junit.Test;
    
    public class ExecutionProcedureJunit {
    	
       //execute only once, in the starting 
       @BeforeClass
       public static void beforeClass(>
    { System.out.println("in before class">
    ; } //execute only once, in the end @AfterClass public static void afterClass(>
    { System.out.println("in after class">
    ; } //execute for each test, before executing test @Before public void before(>
    { System.out.println("in before">
    ; } //execute for each test, after executing test @After public void after(>
    { System.out.println("in after">
    ; } //test case 1 @Test public void testCase1(>
    { System.out.println("in test case 1">
    ; } //test case 2 @Test public void testCase2(>
    { System.out.println("in test case 2">
    ; } }

    TestRunner.java

    
    
    import org.junit.runner.JUnitCore;
    import org.junit.runner.Result;
    import org.junit.runner.notification.Failure;
    
    public class TestRunner {
       public static void main(String[] args>
    { Result result = JUnitCore.runClasses(ExecutionProcedureJunit.class>
    ; for (Failure failure : result.getFailures(>
    >
    { System.out.println(failure.toString(>
    >
    ; } System.out.println(result.wasSuccessful(>
    >
    ; } }


    Output