Testing fundamentals
On this page 8 topics
Overview
Topics
- What testing is
- test cases
- assertions
- boundary conditions
- JUnit introduction
Learn first
74 min of reading · 4 h of practice (estimate)
Learning outcomes
- Explain why automated tests exist and what a good test case contains.
- Write JUnit 5 tests with
@Test, assertions, andassertThrows. - Derive test cases from boundaries and equivalence classes.
- Run tests in the IDE and read failures.
Lessons
Lab and exercises
Lab
BEG-045-LAB1 · Test what you built
Outcomes: 1–4 · Time: ~4 h
- Add JUnit 5 to the project through IntelliJ's library prompt.
- Write tests for
MyArrayList(empty, one item, resize, bad index),MyHashMap(put, replace, missing key, many keys), and the bank'swithdraw(boundaries and exceptions). - Name each test by behavior and follow Arrange, Act, Assert.
- Run all tests in the IDE; fix or plant a bug and show a test catching it.
Acceptance: at least 20 meaningful tests; one bug found or a planted bug caught.
Exercises (assessment: write tests for a provided class with hidden bugs; score = bugs caught)
| ID | Outcome | Task |
|---|---|---|
| BEG-045-EX01 | 1 | Give three reasons automated tests beat checking by hand, and say what a regression is. |
| BEG-045-EX02 | 1 | The test void t() { new Account(10).deposit(5); } has a problem. Name it and fix it. |
| BEG-045-EX03 | 2 | Write a JUnit 5 test for int max(int a, int b) with two cases. |
| BEG-045-EX04 | 2 | Write a test proving Account.withdraw(-1) throws IllegalArgumentException. |
| BEG-045-EX05 | 3 | A method isAdult(int age) returns true from 18 up and rejects negative ages. List the equivalence classes and the boundary inputs. |
| BEG-045-EX06 | 4 | A test reports expected: <3> but was: <2> for list.size() after three adds. List the steps to find the cause. |
Show solutions
- EX01 Faster, repeatable, catches regressions, documents behavior. A regression is a feature that worked and broke after a change.
- EX02 It has no assertion, so it can never fail. Add
assertEquals(15, account.getBalance());after the deposit. - EX03
@Test void returnsLarger() { assertEquals(7, max(3, 7)); } @Test void handlesEqual() { assertEquals(4, max(4, 4)); } - EX04
assertThrows(IllegalArgumentException.class, () -> account.withdraw(-1)); - EX05 Classes: negative (error), 0 to 17 (false), 18 and above (true). Boundaries: -1, 0, 17, 18, 19.
- EX06 Read the message (code returned 2); click the failing line; check the three adds (one may be rejected, or
addis not incrementingsize); fix the code and rerun all tests.
Quiz
Outcomes: O1 why test and test-case anatomy · O2 JUnit 5 · O3 boundaries and equivalence classes · O4 running tests and reading failures.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Write tests for a provided class with hidden bugs (score = bugs caught) plus lab BEG-045-LAB1 (at least 20 meaningful tests) and exercises BEG-045-EX01 to EX06. Pass: at least half of the hidden bugs caught, the lab acceptance met, and at least 8 of 10 quiz answers correct.
Common mistakes
Tests with no assertions; testing only the happy path; tests that depend on each other.
Assessment
Write tests for a provided class with hidden bugs; score = bugs caught.
Career relevance
JNR-043 to JNR-047 and quality engineering throughout.