Use the project's JUnit generation consistently

Inspect existing dependencies and imports before copying a tutorial. JUnit generations have different package and runtime requirements. Use the project's supported test engine and build integration rather than mixing annotations that the runner may not discover, which can create the misleading impression that an empty test run passed successfully.

Start with a plain domain object and a direct assertion, such as an exact invoice total. Keep expected values independent of the production calculation. Framework bootstrapping is unnecessary when the rule does not depend on dependency injection, HTTP handling, or persistence and would only make local failures harder to understand.

Cover boundaries with readable cases

Use parameterized tests when a group of inputs shares one rule and setup. Include equality boundaries, invalid states, and defined error behavior. Give cases meaningful labels where needed. A long list of ordinary successful values adds less evidence than a small set that distinguishes likely comparison or rounding mistakes.

Use lifecycle methods for setup that genuinely repeats across cases, while keeping the decisive input visible in each test. Avoid a large inheritance hierarchy of fixtures. An engineer should be able to read the test and identify the relevant state without tracing several parent classes and hidden global configuration changes.

Keep mocks at external seams

Use a fake or mock for a remote gateway when the local rule needs a controlled success or failure. Assert interactions only when the interaction is itself required behavior, such as sending exactly one charge request. Otherwise prefer assertions on returned values and observable state rather than every internal method call.

Run tests through the normal Maven or Gradle workflow used by the repository. Confirm discovery and retain useful failure output in continuous integration. Add integration checks for real database mapping or framework wiring because an isolated service object with mocked collaborators cannot establish that the assembled application is configured correctly.

Working sheet

Java test boundary choices
RiskUseful testAvoid
Calculation rulePlain JUnit object testFull app startup
Input matrixParameterized casesCopied test bodies
Gateway failureControlled collaboratorLive remote account
Database mappingIntegration testMock-only claim
Download this worksheet (CSV)

Questions about write Java unit tests with JUnit and explicit boundaries

Should every Java unit test start the application framework?

No. Use plain objects when the behavior does not require framework wiring.

Can I mix JUnit examples from different versions?

Check imports, test engine, build integration, and runtime compatibility before doing so.

How should I use the working sheet?

Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.

Sources and further reading

Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.

Make the next change verifiable.

Getzilla is an open source workflow for changes to existing repositories. Inspect the project, define acceptance checks, and keep independent review in the loop.

See the repository workflow See current access and pricing