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.