Test Class Naming Convention Java | Fresher Interview
Fast recall
Quick revision
Core answer
OrderServiceTest for OrderService. Maven Surefire's default includes recognize names such as *Test, Test*, and *Tests; Gradle discovers tests through the configured test framework rather than requiring this suffix. A team may choose another convention if its build is configured for it.Interview-ready guide
Interview answer
A common class name is the production class plus Test: OrderServiceTest for OrderService. It is easy to navigate and matches one of Maven Surefire's default include patterns. Surefire also recognizes names such as TestOrderService and OrderServiceTests, so the suffix is a convention, not a Java or JUnit requirement.
If a team uses a name such as OrderServiceSpec, its build must include that pattern. Integration tests are often named OrderServiceIT, but they run in Maven's integration-test and verify phases only when Failsafe is configured for them. The important check is not the spelling alone: run the build in CI and confirm the expected tests are actually discovered.
Test Class Naming Convention Java | Fresher Interview
Direct Answer
A clear convention is ClassNameTest, such as OrderServiceTest for OrderService. Maven Surefire's default includes recognize names such as *Test, Test*, and *Tests; Gradle discovers tests through the configured test framework rather than requiring this suffix. A team may choose another convention if its build is configured for it.
Code Example
// src/main/java/com/example/order/OrderService.java
package com.example.order;
import java.util.List;
class OrderService {
private final OrderRepository repository;
OrderService(OrderRepository repository) { this.repository = repository; }
Order createOrder(Long customerId, List<String> items) {
if (items.isEmpty())
throw new IllegalArgumentException("Order must have items");
return repository.save(new Order(customerId, items));
}
}
// src/test/java/com/example/order/OrderServiceTest.java (same package)
package com.example.order;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
class OrderServiceTest {
@Test
void createOrder_whenItemsAreProvided_thenSavesOrder() {
var mockRepo = mock(OrderRepository.class);
var service = new OrderService(mockRepo);
service.createOrder(1L, List.of("item1", "item2"));
verify(mockRepo).save(any(Order.class));
}
}
Common Mistakes & Pitfalls
- Assuming every build uses the same pattern. Surefire defaults include both
*TestandTest*; Gradle and IDE discovery follow their own configuration. - Using a custom name without configuring Maven. A class such as
OrderServiceSpecneeds a matching Surefire include or another discovery setup. - Unclear team convention. Several valid patterns become confusing only when their purpose is undocumented or they create duplicate coverage.
Check your understanding
Answer these without looking back at the article.
- Where do test files live in a Maven project?
- What is the difference between Maven Surefire and Failsafe plugins?
- How do you exclude certain tests from the default Maven build?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.