ProCore

Java Testing

What a Java test actually asserts, and where that claim is weaker than it looks. Argument order and failure messages, the three kinds of equality, deltas, throwing, fixtures and static state, test doubles, and the difference between a failure and an error.

Questions90
Topics10
Answering time68 minutes
At five a day18 days
LevelCore

Who it is for

Tests the code they wrote, not the code they meant.

The behaviour people get wrong after they are already productive in the language.

You will get the most out of it if you have written Java recently enough to read it without effort, and have been surprised by it at least once. If you are picking the language up for the first time, start with Java Essentials and come back to this one.

How long it takes

90 graded questions at roughly 45 seconds each is about 68 minutes of answering in total.

At the default five questions a day, and because the session mixes in your other courses, expect around 18 days — call it 3 weeks — to see everything once, plus a few weeks of thinning reviews after that before it is all mastered.

A question is mastered on the third correct answer, once the gap between reviews has grown past a week — so a course cannot be finished in a weekend however long you sit with it.

Question mix

  • 60 multiple choice
  • 10 concept cards
  • 10 predict the output
  • 10 put it in order
  • 10 matching questions

Syllabus

What you will be asked about

  1. 01

    Expected First, Actual Second

    Read a JUnit failure message and say which side is yours.

  2. 02

    Which Equality the Assertion Uses

    Choose between ==, equals, Objects.equals and assertArrayEquals.

  3. 03

    Comparing Doubles With a Delta

    Assert on a floating-point result without pinning down bits nobody meant.

  4. 04

    Asserting That Something Throws

    Write a throw assertion that fails when nothing is thrown.

  5. 05

    BeforeEach, BeforeAll and Fresh State

    Say which fixture code runs once and which runs again for every test.

  6. 06

    Isolation and Static Fields

    Spot the state that survives a test and makes the suite order-dependent.

  7. 07

    Stubs, Mocks and What Each Proves

    Say what a mock verifies that a stub cannot.

  8. 08

    Private Methods and How to Reach Them

    Decide whether a private method needs a test of its own, and what it costs.

  9. 09

    One Test, Many Cases

    Turn a table of cases into tests that each fail on their own.

  10. 10

    A Failure and an Error Are Not the Same

    Tell an assertion that said no from code that fell over, and read the report.

8 more topics, and all 90 questions

Java Testing is a Pro course. One subscription opens it and every other course in the catalogue, including the ones still being written.

See pricing Find a free course

A question from this course

Expected First, Actual Second

This helper prints what JUnit would. What does it print?

public class Main {
  static int total() { return 5; }
  static String eq(Object e, Object a) {
    return "expected: <" + e
      + "> but was: <" + a + ">";
  }
  public static void main(String[] args) {
    System.out.println(eq(total(), 6));
  }
}
  • Aexpected: <total()> but was: <6>
  • BAssertionError: 5 != 6
  • Cexpected: <6> but was: <5>
  • Dexpected: <5> but was: <6>
Show the answer

D expected: <5> but was: <6>

The helper reports its first parameter as expected, and the first argument here is total(), which returns 5. So the message claims the test wanted 5 and the code produced 6 - the exact opposite of the truth. You may have expected the framework to work out which side is the literal; nothing can, because both sides are just values by the time the call is made.

The rest of the track

Java at every depth

FreeFoundation

Java Essentials

The behaviour underneath the ceremony: what a variable really holds, why 1/2 is 0, and why two Strings that look identical are not always the same object. Ten spines on the code that compiles cleanly and still does something else.

90 questions · 68 minutes

ProCore

Java Core

Where Java trips developers who use it every day: autoboxing that hides a NullPointerException, a ternary that quietly unboxes, and dispatch rules that pick a method before the program runs. Ten spines on the gap between what the code says and what the JVM does.

90 questions · 68 minutes

ProCore

Java Data & Collections

The collections framework taken seriously: which container promises what, why remove(1) deletes a position, and how a mutated key disappears from its own HashMap. Ten spines on the containers you use every day and the contracts underneath them.

90 questions · 68 minutes

ProCoreInterview prep

Java Errors & Edge Cases

Exceptions at the points where they stop behaving the way you assumed: a finally block that discards the exception unwinding through it, a map lookup that throws on the way into an int, a class that fails once and reports a missing definition forever after. Ten spines on the paths your happy-path code never takes.

90 questions · 68 minutes

ProCore

Java Standard Library

The library people reimplement by hand: what split really returns, StringBuilder, Optional, streams and collectors, comparators, java.time, the Objects helpers and text blocks. Ten built-ins that already do the job, and one behaviour in each that is not the one you assumed.

90 questions · 68 minutes

ProAdvanced

Java Idioms & Pitfalls

The Java people write out of habit that is wrong or dated: raw types, boxed equality, catching Exception, building strings in a loop, arrays where a List belongs and getters that hand out the field. Ten habits, each one run to see what it actually does.

90 questions · 68 minutes

ProAdvancedInterview prep

Java Advanced

The traps a confident Java reader still walks into: erasure, wildcard variance, dispatch decided before the program runs, and a constructor that sees a field before it is assigned. Ten spines of code that compiles cleanly and behaves otherwise.

90 questions · 68 minutes

ProAdvancedInterview prep

Java Performance

What actually costs on the JVM, read off code rather than a stopwatch: the allocation that never happens, the concat loop that copies everything twice, the map that rehashes sixteen times on its way to a million entries. Ten spines on the difference between the operation you wrote and the work the VM does.

90 questions · 68 minutes

ProAdvancedInterview prep

Java Interview Questions

The Java an interviewer actually probes, answered from the rules rather than from memory. The equals and hashCode contract, the string pool, references and boxing, collections, erasure, checked exceptions, dispatch, immutability, and the what-does-this-print questions behind them.

90 questions · 68 minutes

Start Java Testing
in tomorrow’s session.

Enrol and this course joins your daily five, mixed in with everything else you are studying — 90 questions spread over about 18 days rather than dropped on you at once.

Browse the catalogue