Unravel โบ Java concepts โบ Testing
JUnit tests
A JUnit test is a method annotated with @Test that sets something up, runs the code under test and asserts the expected result.
At a glance
A checklist inspection run after every change: if any item fails, you know before the customer does.
- 1Structure each test as arrange (set up data), act (call the code), assert (check the outcome)assertEquals(expected, actual) takes the expected value first.
- 2@BeforeEach runs before every test to give each a fresh start, so tests do not depend on each other or on order
- 3assertThrows(IllegalArgumentException.class, () -> service.run(null)) checks that a failure happens as designed
- 4@ParameterizedTest runs one test with many inputs, which is good for boundary values
- Test behaviour, not implementation details, and keep tests fast and independent
- JUnit 5 finds tests with a discovery engine, creates a new test class instance per method by default (which is why instance fields do not leak between tests), and reports through the platform launcher used by Maven, Gradle and IDEs
@Testvoid rejectsNegativeAmounts() { PaymentService service = new PaymentService(new FakeGateway()); assertThrows(IllegalArgumentException.class, () -> service.pay(-5));}- line 1 uses Test, the core of JUnit tests
- line 4 uses assertThrows, the core of JUnit tests
Read the full explanation (about 1 min)
A JUnit test is a method annotated with @Test that sets something up, runs the code under test and asserts the expected result. A test that fails when the code is wrong is what makes later changes safe.
A mental model
A checklist inspection run after every change: if any item fails, you know before the customer does.
How it works
Structure each test as arrange (set up data), act (call the code), assert (check the outcome). assertEquals(expected, actual) takes the expected value first.
@BeforeEach runs before every test to give each a fresh start, so tests do not depend on each other or on order.
assertThrows(IllegalArgumentException.class, () -> service.run(null)) checks that a failure happens as designed.
@ParameterizedTest runs one test with many inputs, which is good for boundary values.
Going deeper
Test behaviour, not implementation details, and keep tests fast and independent. @SpringBootTest starts the whole application context and is slow; prefer plain unit tests for logic, and slice tests (@WebMvcTest, @DataJpaTest) for the web or persistence layer.
Under the hood
JUnit 5 finds tests with a discovery engine, creates a new test class instance per method by default (which is why instance fields do not leak between tests), and reports through the platform launcher used by Maven, Gradle and IDEs.
Common mistakes
- Tests that depend on each other's order.
- Swapped expected and actual in
assertEquals. - Tests with no assertions.
- Using the full Spring context for everything.
How Unravel recognises it
When you paste code, Unravel looks for these signs of junit tests. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@Test@BeforeEach@AfterEach@BeforeAll@AfterAll@ParameterizedTest@DisplayName@SpringBootTest@ExtendWith - Types:
Assertions - Method calls:
assertEquals()assertTrue()assertThrows()assertThat()
Part of a learning path
- Reliable code ยท previous: Retry and circuit breaker ยท next: Mockito mocks
- Testing enterprise code ยท next: Advanced JUnit 5 and async assertions
Related concepts
Builds on this
Frequently asked questions
What is JUnit tests?
A JUnit test is a method annotated with @Test that sets something up, runs the code under test and asserts the expected result. A test that fails when the code is wrong is what makes later changes safe.
What are the most common mistakes with JUnit tests?
Tests that depend on each other's order. Swapped expected and actual in assertEquals. Tests with no assertions. Using the full Spring context for everything.
How can I see JUnit tests in real code?
Paste your own Java into Unravel and it will point out where junit tests appears, explain how it works at the detail level you choose, and draw the flow of the code around it.