Test Organization Maven Project Java | Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
In Maven's standard layout, production Java belongs in src/main/java, test Java in src/test/java, and test fixtures such as JSON or SQL in src/test/resources. Tests often use the same package as the code they exercise because that is easy to navigate and allows package-private access, but mirroring is a convention rather than a requirement. Tests may also be grouped by feature when that makes ownership clearer.
Maven Surefire normally runs unit-style test names during mvn test. A project can configure Maven Failsafe to run integration tests during integration-test and verify, often using names such as *IT; the suffix has meaning only with the build plugin's include rules. Package-private access can be useful, but it is not a reason to test every helper directly. Prefer public behavior unless a smaller package-level contract is deliberate. The build configuration is the source of truth for discovery, so verify the executed test count in CI.
Test Organization Maven Project Java | Fresher Interview
Direct Answer
Maven test source code lives in src/test/java and test resources in src/test/resources. Tests often mirror production packages for navigation and package-private access, but they can be organized by feature and do not require one test class per production class. A project may use an *IT convention with Maven Failsafe to separate integration tests when that plugin is configured.
Code Example
// src/main/java/com/example/order/OrderService.java
package com.example.order;
import java.math.BigDecimal;
public class OrderService {
// Package-private — not exposed to external consumers
BigDecimal calculateTax(BigDecimal amount) {
return amount.multiply(new BigDecimal("0.08"));
}
}
// src/test/java/com/example/order/OrderServiceTest.java
package com.example.order; // same package -> can call package-private methods
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class OrderServiceTest {
@Test
void calculateTax_whenAmountIs100_thenReturns8() {
var service = new OrderService();
var tax = service.calculateTax(new BigDecimal("100")); // package-private access
assertEquals(new BigDecimal("8.00"), tax);
}
}
Common Mistakes & Pitfalls
- Wrong directory — tests in
src/main/javaleak JUnit and test code into production builds. - Package mismatch —
OrderServiceTestincom.example.testloses package-private access and obscures which test belongs to which class. - Flat structure — dumping all tests in one root package becomes unnavigable as the codebase grows.
Check your understanding
Answer these without looking back at the article.
- How do you separate unit tests from integration tests in Maven?
- What is the Maven Failsafe plugin?
- How do you load test resources in a Spring Boot test?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.