Site icon TestingDocs.com

Disadvantages of using Inherited tests in JUnit

Introduction

In this tutorial, we will learn why inherited tests in JUnit create lot of confusion. Many people tend to inherit test classes for the sake of reusing the annotated code or methods. There are many  drawbacks using this approach.

Coupling Tests Together

The derived tests tend to run in the frame of the parent class and fragile to any changes to the parent class. We tend to tight couple the tests which are supposed to be independent.

Sample Code

Lets take a sample JUnit test called: JUnit4ParentTestClass
Assume the author of test is authorA.

We have derived a test class from the parent with the name: JUnit4DerivedTestClass with one test method. Assume the author of test is authorB.

package com.testingdocs.junit;
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * A sample JUnit4 Test annotation demo
 * class
 */

/**
 * @author authorA
 *
 */
public class JUnit4ParentTestClass {

  /**
   * @throws java.lang.Exception
   */
  
  @BeforeClass
  public static void oneTimeParentSetUp() throws Exception {
    System.out.println("@BeforeClass : Parent specific implementation 
goes here before class.");
  }


  /**
   * @throws java.lang.Exception
   */
  @AfterClass
  public static void oneTimeParentTearDown() throws Exception {
    System.out.println("@AfterClass : Parent specific implementation 
goes here after class.");
  }

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void eachMethodFixtureSetUp() throws Exception {
    System.out.println("@Before : Parent specific implementation 
goes here before each method.");
  }

  /**
   * @throws java.lang.Exception
   */
  @After
  public void eachMethodFixtureTearDown() throws Exception {
    System.out.println("@After : Parent specific implementation 
goes here after each method.");
  }

  @Test
  public void firstTestMethodParent() {
    System.out.println("Parent Test method 1");
    fail("Not yet implemented1");
  }

  @Test
  public void secondTestMethodParent() {
    System.out.println("Parent Test method 2");
    fail("Not yet implemented2");
  }

}

Derived test

package com.testingdocs.junit;

import static org.junit.Assert.fail;
import org.junit.Test; 

//Sample JUnit Derived TestClass 
//BaseClass : JUnit4ParentTestClass 

/** * @author authorB * */ 

public class JUnit4DerivedTestClass extends JUnit4ParentTestClass {

 @Test public void firstTestMethodDerived() { 

System.out.println("Derived Test method 1");
 fail("Not yet implemented1"); 
} 

}

 

Run the test in the inherited test class JUnit4DerivedTestClass. The output is shown below:

Output:

@BeforeClass : Parent specific implementation goes here before class.
@Before : Parent specific implementation goes here before each method.
Derived Test method 1
@After : Parent specific implementation goes here after each method.
@Before : Parent specific implementation goes here before each method.
Parent Test method 1
@After : Parent specific implementation goes here after each method.
@Before : Parent specific implementation goes here before each method.
Parent Test method 2
@After : Parent specific implementation goes here after each method.
@AfterClass : Parent specific implementation goes here after class.

 

Overhead of Test Runs

JUnit actually runs all the annotated methods in the parent class and overhead of the tests in that class. Any changes to the parent methods  would make your tests fragile.

For example, authorA has changed the configuration annotated methods in the parent class. To show this, we have <Change> to the method outputs.

@BeforeClass
  public static void oneTimeParentSetUp() throws Exception {
    System.out.println("@BeforeClass : Parent specific implementation 
<Change> goes here before class.");
  }

 

Now when the derived class tests run:

Tight Coupling

Without any changes to your tests in derived class, your tests have become dependent on the methods of the parent class and to the changes to that class.

JUnit Tutorials on this website:

https://www.testingdocs.com/junit-tutorial/

More Information on JUnit

https://junit.org/junit5/

Exit mobile version