liveBook · Manning

Test

As explained in 100 Java Mistakes and How to Avoid Them

A test case is a defined set of inputs, execution conditions, and expected results for evaluating software behavior.

Testing evaluates software behavior with defined inputs, execution conditions, and expected results. Automated tests reduce bugs and expose regressions, but cannot prove a program is bug-free. Use them alongside static analysis and code review rather than treating any one technique as sufficient. section 1.4 section 1.8

Unit tests matter only when they actually execute and make meaningful checks. A faulty test can pass while testing nothing, leaving a regression undetected. Assertions should express the intended behavior precisely; weak checks such as non-nullness or inequality can pass despite an incorrect result. section 10.0 section 10.8

Build tests around observable behavior and failure-prone boundaries:

  • Cover every branch of an if chain, and run loops with two or more iterations when repeated execution can change state. section 3.12
  • Test string-processing functions even with a simple case; argument-order mistakes can compile and run without throwing exceptions. section 6.10
  • Test utility methods thoroughly and reuse them instead of repeating a fragile inline formula, especially when inputs may be negative. section 4.5
  • Cover UI messages with unit tests to catch library-call mistakes that produce visibly wrong output. section 9.1

Avoiding a test that silently skips assertions

A test that catches an expected exception and returns exits before later assertions run. Use assertThrows() for the exceptional operation, then keep follow-up verification reachable; if the checks concern separate behavior, split them into separate tests. section 10.4 section 10.8

public void testMapping() throws Exception {
  E e = new E();
  e.mymap.put("a.b", "a");
  e.mymap.put("c.e.g", "b");

  try {
    getDs().save(e);
  } catch (Exception ex) {
    return; #1
  }

  Assert.assertFalse("Should have got rejection…", true);
  e = getDs().get(e);
  Assert.assertEquals("a", e.mymap.get("a.b")); #2
  Assert.assertEquals("b", e.mymap.get("c.e.g"));
}

The return in the catch block makes the later map assertions unreachable whenever save() throws, so the test appears successful without verifying them. section 10.4

public void testMapping() {
  E e = new E();
  e.mymap.put("a.b", "a");
  e.mymap.put("c.e.g", "b");

  assertThrows(Exception.class, () -> getDs().save(e));
  E restoredE = getDs().get(e);     #1
  Assert.assertEquals("a", restoredE.mymap.get("a.b"));
  Assert.assertEquals("b", restoredE.mymap.get("c.e.g"));
}

assertThrows() states the expected failure directly and leaves subsequent restoration checks visible and executable. section 10.4

Property-based testing performs random operations and checks invariants such as absence of unexpected exceptions or preservation by read-only operations. When a property fails, the framework can replay and shrink the operation sequence. Coverage tools report executed lines, instructions, or branches, but high coverage is costly near 100% and remains an incomplete quality measure. section 1.4 section 1.8

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains practical test design, execution traps, property testing, and coverage limits without teaching a complete testing methodology.
Examples
Demonstrates a prematurely returning exception test, branch and loop cases, string processing, oddness checks, and UI messages.
What it assumes
Assumes Java programming knowledge and focuses on writing valid, executed unit tests rather than introductory language syntax.
What you can do after
Apply it via JUnit-style assertions and automated-test strategies to catch regressions and reveal common Java mistakes.
How it connects
Connects testing with static analysis, code review, assertions, code coverage, mutation coverage, and property-based testing.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.