How to Create a Base Test Class in Java
When writing unit tests in Java, you often find that multiple test classes share common setup or utility methods. Instead of duplicating code, you can create a base test class that other test classes can extend. This improves maintainability, readability, and reusability.
1️⃣ Why Use a Base Test Class?
Reduce code duplication
Centralize common setup/teardown logic
Share mock objects, utilities, or configurations
Standardize testing behavior across multiple test classes
2️⃣ Using JUnit 5 (Recommended)
JUnit 5 is the modern testing framework in Java.
You can define a base test class like this:
import org.junit.jupiter.api.BeforeEach;
public abstract class BaseTest {
protected MyService myService;
@BeforeEach
void setUp() {
// Initialize common resources
myService = new MyService();
System.out.println("Base setup executed.");
}
// Common utility methods
protected void logTestStart(String testName) {
System.out.println("Starting test: " + testName);
}
}
Notes:
abstract keyword prevents direct instantiation of the base class.
@BeforeEach ensures the setup runs before each test in derived classes.
You can add common utility methods that all child tests can use.
3️⃣ Creating a Derived Test Class
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MyServiceTest extends BaseTest {
@Test
void testServiceLogic() {
logTestStart("testServiceLogic");
int result = myService.calculate(2, 3);
assertEquals(5, result);
}
}
How it works:
MyServiceTest inherits myService and setUp() from BaseTest.
logTestStart utility method can be reused.
Each test automatically benefits from the shared setup logic.
4️⃣ Advanced: Using @BeforeAll / @AfterAll
If you have setup that should run once per test class, use @BeforeAll and @AfterAll:
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
public abstract class BaseTest {
@BeforeAll
static void globalSetup() {
System.out.println("Global setup before all tests");
}
@AfterAll
static void globalTeardown() {
System.out.println("Global cleanup after all tests");
}
}
Notes:
Methods must be static in JUnit 5.
Useful for initializing databases, test containers, or logging frameworks.
5️⃣ Common Use Cases for Base Test Classes
Service Layer Tests: Initialize mocks or test data
Controller Tests: Setup MockMvc or HTTP clients
Integration Tests: Setup in-memory databases, test containers
API Testing: Setup authentication tokens or base URL configuration
6️⃣ Best Practices
Keep the base class generic and reusable
Avoid including too many unrelated methods
Use abstract class instead of concrete class to prevent accidental instantiation
Use protected fields and methods so child classes can access them
Prefer composition over inheritance if multiple unrelated base behaviors are needed
✅ Summary
Create an abstract base test class for shared setup and utilities.
Use @BeforeEach or @BeforeAll for setup logic.
Extend the base class in your test classes.
Add reusable utility methods to reduce code duplication.
Result: Cleaner, more maintainable, and DRY (Don’t Repeat Yourself) test code.
Learn Selenium with JAVA Training in Hyderabad
Read More
Reusable Methods in Java for Selenium Tests
How to Use Java Streams in Selenium Automation
Reading Data from Properties Files in Java
Creating Utility Classes for Common Selenium Functions
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments