Close

JUnit - Inheritance of Test classes

[Last Updated: Jul 8, 2017]

In this tutorial, we will analyze the JUnit test class behavior when extending another test class.

Example

public class MyBaseClass {

  @BeforeClass
  public static void beforeBaseClassMethod() {
      System.out.println("MyBaseClass: @BeforeClass static method invoked.");
  }

  @Before
  public void beforeBaseMethod() {
      System.out.println("MyBaseClass: @Before method invoked.");
  }

  @After
  public void afterBaseMethod() {
      System.out.println("MyBaseClass: @After method invoked.");
  }

  @Test
  public void testBaseMethod1() {
      System.out.println("MyBaseClass: in testMethod1()");
  }
}
public class MySubClass extends MyBaseClass {

  @BeforeClass
  public static void beforeSubClassMethod() {
      System.out.println("@BeforeClass static method invoked. MySubClass");
  }

  @Before
  public void beforeSubMethod() {
      System.out.println("MySubClass: @Before method invoked.");
  }

  @After
  public void afterSubMethod() {
      System.out.println("MySubClass: @After method invoked.");
  }

  @Test
  public void testMethod2() {
      System.out.println("MySubClass: in testMethod2().");
  }
}
mvn -q test -Dtest=MySubClass

Output

d:\example-projects\junit\test-class-inheritance>mvn -q test -Dtest=MySubClass

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MySubClass
MyBaseClass: @BeforeClass static method invoked.
@BeforeClass static method invoked. MySubClass
MyBaseClass: @Before method invoked.
MySubClass: @Before method invoked.
MySubClass: in testMethod2().
MySubClass: @After method invoked.
MyBaseClass: @After method invoked.
MyBaseClass: @Before method invoked.
MySubClass: @Before method invoked.
MyBaseClass: in testMethod1()
MySubClass: @After method invoked.
MyBaseClass: @After method invoked.
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

If we use the same lifecycle and test method names then of course polymorphism will be applied i.e. only overridden methods will be invoked. The static @BeforeClass will also be invoked only once for the subclass:

public class MyBaseClass {

  @BeforeClass
  public static void beforeClassMethod() {
      System.out.println("MyBaseClass: @BeforeClass static method invoked.");
  }

  @Before
  public void beforeMethod() {
      System.out.println("MyBaseClass: @Before method invoked.");
  }

  @After
  public void afterMethod() {
      System.out.println("MyBaseClass: @After method invoked.");
  }

  @Test
  public void testMethod1() {
      System.out.println("MyBaseClass: in testMethod1()");
  }
}
public class MySubClass extends MyBaseClass {

  @BeforeClass
  public static void beforeClassMethod() {
      System.out.println("@BeforeClass static method invoked. MySubClass");
  }

  @Before
  public void beforeMethod() {
      System.out.println("MySubClass: @Before method invoked.");
  }

  @After
  public void afterMethod() {
      System.out.println("MySubClass: @After method invoked.");
  }

  @Test
  public void testMethod1() {
      System.out.println("MySubClass: in testMethod1().");
  }
}
mvn -q test -Dtest=MySubClass

Output

d:\example-projects\junit\test-class-inheritance-2>mvn -q test -Dtest=MySubClass

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MySubClass
@BeforeClass static method invoked. MySubClass
MySubClass: @Before method invoked.
MySubClass: in testMethod1().
MySubClass: @After method invoked.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Example Project

Dependencies and Technologies Used:

  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Junit Inheritance Example Select All Download
  • test-class-inheritance
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • MySubClass.java

    See Also