Skip to content
Ameen Abozaid
BEG-045

Testing fundamentals

  • 7 Lessons
  • 74 min of reading
  • 4 h practice (estimate)
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

  1. Explain why automated tests exist and what a good test case contains.
  2. Write JUnit 5 tests with @Test, assertions, and assertThrows.
  3. Derive test cases from boundaries and equivalence classes.
  4. Run tests in the IDE and read failures.

Lessons

  1. 01Why test automatically11 min read
  2. 02Anatomy of a test case10 min read
  3. 03First JUnit 5 tests11 min read
  4. 04Testing exceptions10 min read
  5. 05Boundaries and equivalence classes10 min read
  6. 06Reading test failures11 min read
  7. 07Demo: test-first on the bank's withdraw method11 min read

Lab and exercises

Lab

BEG-045-LAB1 · Test what you built

Outcomes: 1–4 · Time: ~4 h

  1. Add JUnit 5 to the project through IntelliJ's library prompt.
  2. Write tests for MyArrayList (empty, one item, resize, bad index), MyHashMap (put, replace, missing key, many keys), and the bank's withdraw (boundaries and exceptions).
  3. Name each test by behavior and follow Arrange, Act, Assert.
  4. 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)

IDOutcomeTask
BEG-045-EX011Give three reasons automated tests beat checking by hand, and say what a regression is.
BEG-045-EX021The test void t() { new Account(10).deposit(5); } has a problem. Name it and fix it.
BEG-045-EX032Write a JUnit 5 test for int max(int a, int b) with two cases.
BEG-045-EX042Write a test proving Account.withdraw(-1) throws IllegalArgumentException.
BEG-045-EX053A method isAdult(int age) returns true from 18 up and rejects negative ages. List the equivalence classes and the boundary inputs.
BEG-045-EX064A 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 add is not incrementing size); 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.

  1. 1A regression is:O1
  2. 2The three steps of a test are:O1
  3. 3A test with no assertion:O1
  4. 4The annotation that marks a JUnit 5 test method is:O2
  5. 5In assertEquals(a, b) the first argument should be the:O2
  6. 6To check that code throws IllegalArgumentException use:O2
  7. 7A boundary test for "age must be at least 18" should include:O3
  8. 8An equivalence class is a group of inputs that:O3
  9. 9Which tests a method best?O3
  10. 10expected: <5> but was: <4> means the code returned:O4

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.

Type to search levels, phases, modules and lessons.