What if your code could check itself every time you change it?
Why First JUnit test? - Purpose & Use Cases
Imagine you have a calculator app, and every time you change something, you have to open the app and press buttons yourself to check if it still adds numbers correctly.
This manual checking is slow and tiring. You might forget some tests or make mistakes while testing. It's easy to miss bugs, especially when the app grows bigger.
Writing your first JUnit test means you create a small program that automatically checks if your calculator adds numbers right. You run it anytime, and it tells you if something breaks.
Open app -> Press 2 + 3 -> Check if result is 5
@Test
void testAddition() {
assertEquals(5, calculator.add(2, 3));
}Automated tests let you quickly and safely check your code, so you can fix bugs early and build better software.
A developer changes the calculator's code and runs the JUnit test. The test fails, showing the developer immediately where the problem is, saving hours of manual checking.
Manual testing is slow and error-prone.
JUnit tests automate checking your code.
Automated tests help catch bugs early and save time.
Practice
@Test?Solution
Step 1: Understand the role of
The@Testannotation@Testannotation marks a method as a test method in JUnit, meaning it will be run to check code behavior.Step 2: Identify the purpose of test methods
Test methods verify if the code produces the expected results, helping find bugs or confirm correctness.Final Answer:
To check if a piece of code works as expected -> Option AQuick Check:
JUnit@Testmarks test methods [OK]
- Thinking @Test creates classes
- Confusing test methods with print statements
- Using @Test to declare variables
Solution
Step 1: Check for @Test annotation
JUnit 5 requires the@Testannotation to mark a method as a test.Step 2: Verify method signature and assertion
The method should be public void with an assertion likeassertEquals(expected, actual). @Test public void testMethod() { assertEquals(5, 2 + 3); } matches this.Final Answer:
@Test public void testMethod() { assertEquals(5, 2 + 3); } -> Option CQuick Check:
JUnit test method needs @Test and assertion [OK]
- Missing @Test annotation
- Wrong method visibility or return type
- Using print statements instead of assertions
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class SampleTest {
@Test
void additionTest() {
assertEquals(10, 5 + 5);
}
}Solution
Step 1: Analyze the assertion
The test asserts that 5 + 5 equals 10, which is true.Step 2: Check for syntax and imports
All necessary imports and syntax are correct, so the test will run without errors.Final Answer:
Test passes successfully -> Option BQuick Check:
Correct assertion means test passes [OK]
- Assuming test fails when assertion is true
- Missing static import for assertions
- Confusing compilation errors with runtime errors
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalcTest {
@Test
public void testSum() {
int result = 2 + 2;
assertEquals(5, result);
}
}Solution
Step 1: Check the assertion values
The test expects 5 but the actual result is 4 (2 + 2), so the assertion will fail.Step 2: Verify annotations and imports
The @Test annotation and imports are correct, so no syntax errors exist.Final Answer:
The assertion expects wrong value causing test failure -> Option AQuick Check:
Assertion mismatch causes test failure [OK]
- Ignoring assertion expected value
- Thinking missing annotation causes failure here
- Confusing method return types in tests
multiply(int a, int b) returns 0 when one input is 0. Which test method is correct?Solution
Step 1: Confirm @Test annotation and method signature
The test method must be annotated with@Testand return void. @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } meets this.Step 2: Check assertion correctness
The method should assert that multiplying by zero returns zero. @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } assertsassertEquals(0, multiply(0, 5)), which is correct.Final Answer:
@Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } -> Option DQuick Check:
Use @Test, void method, correct assertion [OK]
- Omitting @Test annotation
- Using wrong return type in test method
- Asserting wrong expected value
Ready to prove it?
Pass the test and this pattern moves unknown
