Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestSuite Setup in jUnit 4

People also ask

What is setup () in JUnit?

First, JUnit 4 has a setup method that is invoked before each test method. This method is typically used for creating and configuring the system under test. This means that: We should create the dependencies of the tested object in this method.

What is TestSuite in JUnit?

Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests. This chapter takes an example having two test classes, TestJunit1 & TestJunit2, that run together using Test Suite.


Here is what I have and it runs just fine.

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ TestSuite1.class, TestSuite2.class })
public class CompleteTestSuite {

    @BeforeClass 
    public static void setUpClass() {      
        System.out.println("Master setup");

    }

    @AfterClass public static void tearDownClass() { 
        System.out.println("Master tearDown");
    }

}

Here is my test suite 1 (do the same for test suite 2).

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(value = Suite.class)
@SuiteClasses(value = { TestCase1.class })
public class TestSuite1 {}

And here is my test class. Create both testcase1 and testcase2.

import static org.junit.Assert.assertEquals;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestCase1 {

    @BeforeClass 
    public static void setUpClass() {      
        System.out.println("TestCase1 setup");
    }

    @Test
    public void test1() {
        assertEquals(2 , 2);
    }
}    

you should have 5 classes completesuite suite1 suite2 test1 test2

and make sure you have Junit in your build path. This should run!

Here is the output

Master setup
TestCase1 setup
Master tearDown