Mixin test cases with JUnit 5 and default methods

When implementing E2E tests using JUnit in the test runner, I divided the test class for each screen and API. At this time, I considered how to define a test method that is common to multiple test classes.

(1) Use inheritance relationship

By defining a common test method in the parent class of the test class, the test method of the parent class is also executed when each test class is executed.

The behavior peculiar to each test class is defined as an interface (or an abstract method of the parent class), and the common test method uses this interface to implement the test.

public abstract class CommonTestA {
	//Abstract methods for defining behaviors specific to test classes
    abstract SomeObject doSomething();
    
    @Test
    public void commonTestX() {
        // doSomething()Implement common test cases using
    }
}
//CommonTest when running this test class.commonTestX()Is also executed
public class TestClass extends CommonTestA {
    //Implement test cases specific to the test class
}

With this method, one test class can inherit only one common test class due to Java inheritance restrictions, which is a problem when you want to create multiple common test classes.

(2) Use the default method

You can avoid the problem (1) by using the default method of the interface introduced in Java 8. (Because multiple interfaces can be implemented in one class.) However, in JUnit 4 and earlier, the default method is not executed in the implementation class, so you need to use JUnit 5.

public interface CommonTestA {
    SomeObject doSomething();

    @Test
    default void commonTestA() {
        //Define a common test method as the default method
    }
}
//You can implement multiple common test interfaces in one test class
public class TestClassX implements CommonTestA, ComonTestB {
	// ...
}

Recommended Posts

Mixin test cases with JUnit 5 and default methods
[Java] Test private methods with JUnit
Unit test with Junit.
Test Web API with junit
Test private methods in JUnit
Test private methods in JUnit
Studying Java 8 (StaticIF and Default methods)
Copy and paste test with RSpec
Test Spring framework controller with Junit
Control test order in Junit4 with enumeration
How to test private methods with arrays or variadic arguments in JUnit
How to test private scope with JUnit
JUnit 5 gradle test fails with lombok annotation
[Java] How to test for null with JUnit
[CircleCI 2.0] [Java] [Maven] [JUnit] Aggregate JUnit test results with CircleCI 2.0
Supports multiple test cases with one method with @ParameterizedTest!
[Ruby] Arguments with keywords and default values of arguments
How to execute and mock methods using JUnit
Java automated test implementation with JUnit 5 + Apache Maven
How to test interrupts during Thread.sleep with JUnit
Build and test Java + Gradle applications with Wercker
Easy JUnit test of Elasticsearch 2018 version with embedded-elasticsearch
Test code using mock with JUnit (EasyMock center)
Visualize test methods running in TestNG with listeners
Functions and methods
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Test the contents of an Excel file with JUnit
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Test Web API with junit
I wrote a test with Spring Boot + JUnit 5 now