JUnit Assertions & Execution
Learn to use JUnit's assertion methods to validate test outcomes and run tests effectively within your IDE.
JUnit Assertions & Execution is a free Testing Mastery: JUnit, Mockito & Integration Tests lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Testing Mastery: JUnit, Mockito & Integration Tests learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Assertions?
Assertions are the checks that decide pass or fail: they verify a condition is true, and if it isn’t, the test fails and flags the problem.
Checking for Equality
assertEquals() is the workhorse: pass it the expected value and the actual value, and the test passes only when they match.
`assertEquals` in Action
Here’s assertEquals verifying a sum: compare the expected result against what your method returns. Match means pass.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MathUtilsTest {
// A simple method to test
int add(int a, int b) {
return a + b;
}
@Test
void testAddMethod() {
// Expected result: 5
int expected = 5;
// Actual result from our method
int actual = add(2, 3);
// Assert that expected and actual are the same
assertEquals(expected, actual, "2 + 3 should be 5");
}
}Checking Boolean Conditions
To check logical outcomes, use assertTrue and assertFalse — they pass when the condition is true or false respectively.
`assertTrue` in Practice
Here assertTrue confirms a number is positive — a clean way to validate any boolean condition.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NumberCheckerTest {
// A simple method to test
boolean isPositive(int number) {
return number > 0;
}
@Test
void testIsPositive() {
int num = 10;
// Assert that num is positive
assertTrue(isPositive(num), num + " should be positive");
}
}Handling Null Values
For null checks, use assertNotNull and assertNull — crucial for catching NullPointerExceptions before they bite.
`assertNotNull` Demo
Here assertNotNull and assertNull verify a lookup that returns a user or null when none is found.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class UserServiceTest {
// Dummy method simulating fetching a user
String getUserById(int id) {
if (id == 1) {
return "Alice"; // User found
}
return null; // User not found
}
@Test
void testExistingUser() {
String user = getUserById(1);
assertNotNull(user, "User with ID 1 should exist");
}
@Test
void testNonExistingUser() {
String user = getUserById(99);
assertNull(user, "User with ID 99 should not exist");
}
}Executing JUnit Tests
Running tests is easy: most IDEs have built-in JUnit support — right-click a class or method, hit Run, and read the results window.
Understanding Pass/Fail
Reading results is simple: green means all tests passed, red means an assertion failed and there’s likely a bug. Click a failure for details.
Assertion Challenge
Consider the following simple method and test. What will be the outcome of running this test?
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SimpleCalculatorTest {
int subtract(int a, int b) {
return a - b;
}
@Test
void testSubtract() {
assertEquals(10, subtract(15, 5));
}
}Recap: Assertions & Execution
You can now validate code with JUnit assertions — assertEquals, assertTrue, assertNull and friends — and run tests right in your IDE.
Frequently asked questions
Is the “JUnit Assertions & Execution” lesson free?
Yes — the full text of “JUnit Assertions & Execution” is free to read here on the web, and the Testing Mastery: JUnit, Mockito & Integration Tests course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Testing Mastery: JUnit, Mockito & Integration Tests course, upgrade to CoddyKit PRO.
What will I learn in “JUnit Assertions & Execution”?
Learn to use JUnit's assertion methods to validate test outcomes and run tests effectively within your IDE. You practise Testing Mastery: JUnit, Mockito & Integration Tests with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Testing Mastery: JUnit, Mockito & Integration Tests?
No prior experience is required. Testing Mastery: JUnit, Mockito & Integration Tests on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “JUnit Assertions & Execution” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Testing Mastery: JUnit, Mockito & Integration Tests lesson?
Yes. Every Testing Mastery: JUnit, Mockito & Integration Tests lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Unit Testing
- Basic JUnit 5 Annotations
- JUnit Assertions & Execution
- Organizing Tests with @DisplayName and Nesting