Notes
The layout
Maven expects a standard layout, so every Java project looks the same. The pom dot xml file at the top describes the project. Code goes under source, main, java. Tests go under source, test, java, in the same package as the code they test. Maven writes everything it builds into a folder called target, which you never commit to Git. Learn this layout once and you can open any Java test repository and find your way around.
shoprung-tests/
pom.xml
src/main/java/
dev/algorung/shop/
Coupon.java
src/test/java/
dev/algorung/shop/
PricingTest.java
target/ (build output,
never commit) pom.xml
The pom names the project with a group id and an artifact id, sets the Java version to twenty one, and lists dependencies. Here there is one: JUnit Jupiter, version five point eleven point four, with test scope, which means it is only used by test code. When you build, Maven downloads it from Maven Central into a cache on your computer. Always pin exact versions, so the suite behaves the same on every machine and in CI.
<groupId>dev.algorung</groupId> <artifactId>shoprung-tests </artifactId> <maven.compiler.release>21 <dependency> <groupId>org.junit.jupiter <artifactId>junit-jupiter <version>5.11.4</version> <scope>test</scope> </dependency>
Plugins
Maven itself does little. Plugins do the work. The compiler plugin compiles your code. The Surefire plugin runs the tests: by default it finds classes whose names end in Test and runs them with JUnit five. Pin plugin versions too. Old Surefire versions do not see JUnit five tests at all, which leads to a confusing build success with zero tests run. If you ever see tests run zero, check this first.
<plugin> maven-compiler-plugin <version>3.13.0</version> </plugin> <plugin> maven-surefire-plugin <version>3.5.2</version> </plugin> Surefire finds *Test.java and runs them with JUnit 5
Life cycle
Maven has a build life cycle. Running m v n test runs every phase up to test: validate, compile the code, compile the tests, then run them. Add dash D test equals a class name to run one class, or a hash and a method name to run one test. m v n clean test deletes the target folder first, for a fresh build. At the end, Surefire prints a summary line: tests run, failures, errors and skipped.
mvn test validate → compile → test-compile → test mvn test -Dtest=PricingTest mvn test -Dtest=PricingTest# cartTotals mvn clean test (fresh build) mvn -q test (quiet) [INFO] Tests run: 19, Failures: 0, Errors: 0
A JUnit test
A JUnit test is a method marked with the at test annotation. Good tests follow three steps: arrange the data, act by calling the code, and assert the result. Four notebooks make five thousand cents, save ten's discount on that is five hundred, and assert equals checks it. Note the argument order: expected first, actual second. Swap them and the failure message tells the wrong story. Give tests names that read like the requirement.
class PricingTest {
@Test
void save10AtExactlyFifty() {
long sub = 4 * 1250; // arrange
long d = Coupon.SAVE10
.discount(sub); // act
assertEquals(500, d); // assert
}
}
// assertEquals(expected, actual) Assertions
Pick the assertion that gives the clearest failure. Assert equals for exact values, assert true and assert null for conditions. Assert all groups several checks and reports every failure, not just the first, which is ideal for a cart's subtotal, shipping and total. Assert throws checks that code refuses bad input with the right exception, like a quantity of eleven, and returns the exception so you can check its message too.
assertEquals(500, d);
assertTrue(total > 0);
assertNull(problem);
assertAll("totals",
() -> assertEquals(5000, sub),
() -> assertEquals(499, ship),
() -> assertEquals(4999, tot));
var e = assertThrows(
IllegalArgumentException.class,
() -> Cart.quantity(11)); Parameterized
Parameterized tests run one test method with many rows of data, which is exactly how boundary values should be automated. CSV source gives each row: forty nine ninety nine gets no discount, fifty dollars gets five, fifty oh four gets five, and fifty oh five gets five dollars and one cent, the rounding edge. Each row appears as its own test in the report, named with the pattern you give, so a failure says exactly which boundary broke.
@ParameterizedTest(name =
"{0} cents -> discount {1}")
@CsvSource({
"4999, 0",
"5000, 500",
"5004, 500",
"5005, 501"
})
void save10(long sub, long d) {
assertEquals(d, SAVE10
.discount(sub)); } Lifecycle hooks
JUnit has hooks that run around tests. Before each and after each run around every test; before all and after all run once per class and must be static. A common use: before each test, reset ShopRung's data, so every test starts clean and tests never depend on each other. Display name gives a readable name, and disabled skips a test. Always say why it is disabled, with a bug id, or it will stay disabled forever.
@BeforeAll static, once
@BeforeEach before every test
@AfterEach after every test
@AfterAll static, once
@BeforeEach void reset() {
api.post("/api/test/reset");
}
@DisplayName("SAVE10 rules")
@Disabled("bug B-7") // say why Reading failures
When a test fails, read the failure from the top. It names the test and line, the parameter row that failed, and expected versus actual: expected five hundred but was zero. That is the off by one bug at fifty dollars. The build ends in build failure, which also fails the pipeline. A failure means an assertion was wrong; an error means the test itself crashed with an exception. Detailed reports land in target, surefire reports.
[ERROR] Failures: [ERROR] PricingTest.save10:52 4999 cents -> discount 0 5000 cents -> discount 500 expected: <500> but was: <0> [ERROR] Tests run: 19, Failures: 1, Errors: 0 [INFO] BUILD FAILURE reports: target/surefire-reports
Good tests
Rules that keep a suite fast and trustworthy. Test one behaviour per test, with a name that reads like the rule. Never let one test depend on another having run first. No thread sleep. Exact expected values. And see each test fail at least once: break the code on purpose, run it, then put it back. A test you have never seen fail may not be testing anything. In lab four you do exactly that with the coupon boundary.
- one behaviour per test
- names that read like rules
- no test depends on another
- no Thread.sleep
- exact expected values
- see each test FAIL once: break the code on purpose
- fast unit tests, many; slow UI tests, few
- pin every version
Common mistakes
Mistakes in Java test projects. Test classes whose names do not end in test, so Surefire never runs them. An old Surefire that runs zero tests. Assert equals with actual and expected swapped. JUnit four and five imports mixed in one class. Tests placed under source main instead of source test. Committing the target folder. And unpinned versions that change under your feet.
- test classes not ending in Test → never run
- old Surefire → 0 tests run
- assertEquals(actual, expected)
- JUnit 4 and 5 imports mixed
- tests in src/main/java
- committing target/
- unpinned versions
Say it like this
In the interview: my project is a Maven project with a pom that pins JUnit five and Surefire, and tests under source test java. m v n test runs the suite locally and in CI. Tests follow arrange, act, assert, are independent, use parameterized tests for boundaries and assert all for related checks, and reset data in before each. If asked about TestNG, say it is common in Selenium suites and comes later in Part seven.
- "A Maven project: pom.xml with pinned JUnit 5 and Surefire, tests under src/test/java.
- mvn test runs locally and in CI. Tests are arrange-act- assert, independent, with parameterized tests for boundaries, assertAll for related checks, and setup in @BeforeEach."