JUnit 5 Assertions | Java Fresher Interview
Fast recall
Quick revision
Core answer
org.junit.jupiter.api.Assertions: `assertEquals(expected, actual)` for value equality (expected first!), `assertTrue`/`assertFalse`, `assertNull`/`assertNotNull`, `assertThrows` to verify an exception, `assertAll` to group assertions, and `assertSame` for reference identity. Most teams layer AssertJ's fluent assertThat(x).isEqualTo(y) on top for readability.Interview-ready guide
Interview answer
In JUnit 5, assertions are static methods on the Assertions class, and I usually import them statically so I can just call assertEquals directly. The one I use most is assertEquals, and the important detail is that the expected value comes first and the actual value second. That ordering matters because the failure message reads expected this but was that — if you swap them, the message is backwards and misleads you while debugging.
Beyond assertEquals, there's assertTrue and assertFalse for booleans, assertNull and assertNotNull for references, and assertThrows for exceptions. assertThrows is really useful because it doesn't just check that an exception was thrown — it returns the exception object, so I can then assert on its message or its cause. That's much cleaner than the old try-catch-then-fail pattern.
For example, assertEquals(10, calculator.add(3, 7)) verifies a returned value, while assertThrows(IllegalArgumentException.class, () -> account.withdraw(-1)) verifies an invalid call and returns the exception for further checks.
There's also assertAll, which lets me group several assertions together. Normally separate assertions stop at the first failure, but assertAll runs all of them and reports every failure at once, which is handy when I'm checking several fields of one object. And there's assertSame, which checks reference identity with double-equals rather than value equality — I rarely need it.
One gotcha is calling assertEquals on objects that don't override equals — it falls back to reference comparison and fails even when the values match. In practice a lot of teams add AssertJ on top, so you write assertThat(price).isEqualTo(ninety), which reads more naturally for complex objects and collections.
JUnit 5 Assertions | Java Fresher Interview
Direct Answer
JUnit 5 assertions are static methods on org.junit.jupiter.api.Assertions: assertEquals(expected, actual) for value equality (expected first!), assertTrue/assertFalse, assertNull/assertNotNull, assertThrows to verify an exception, assertAll to group assertions, and assertSame for reference identity. Most teams layer AssertJ's fluent assertThat(x).isEqualTo(y) on top for readability.
Code Example
import static org.junit.jupiter.api.Assertions.*;
@Test
void coreAssertions() {
assertEquals(5, calc.add(2, 3)); // expected first
assertTrue(calc.add(2, 3) > 0);
assertNotNull(repo.findById(1L));
// assertThrows returns the thrown exception for further checks
var ex = assertThrows(IllegalArgumentException.class,
() -> calc.divide(1, 0));
assertEquals("divisor must not be zero", ex.getMessage());
// assertAll reports ALL failures, not just the first
var p = new Point(2, 3);
assertAll("point",
() -> assertEquals(2, p.x()),
() -> assertEquals(3, p.y()));
}
Common Mistakes & Pitfalls
- Expected/actual swapped.
assertEquals(actual, expected)flips the message to "expected <yours> but was <correct>", which misleads during debugging. - try/catch for exceptions. Wrapping in try/catch and calling
fail()is verbose and error-prone; useassertThrows. assertEqualson objects with noequals(). It falls back to reference identity and fails unexpectedly — overrideequalsor compare fields.- Multiple asserts that stop early. Separate
assertEqualscalls stop at the first failure;assertAllshows all.
Check your understanding
Answer these without looking back at the article.
- How do you test that a method throws a specific exception?
- What is assertAll and when should you use it?
- What is the difference between assertEquals and assertSame?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.