← Bob Recipes

Create unit tests for a Java application

Generate meaningful JUnit tests with real boundary coverage instead of coverage padding.

Beginner25 min per classCOMMUNITY ANALYSISJavaJUnitMockito

Prompt

Generate JUnit 5 tests for the attached class.
First list the test cases you intend to write: happy path, boundaries, null/empty, error handling, concurrency if relevant.
Wait for my confirmation, then write the tests using the project's existing conventions and Mockito for collaborators.
Finally list behaviour you could not test and why.

Problem

Generated tests default to trivial assertions unless you force a case list first.

Goal

A test class with named cases per scenario and an explicit gap list.

Context

An existing Maven or Gradle project with JUnit 5 already configured.

Expected Output

Case list, then test code, then an untestable-behaviour list.

Steps

  • Attach one class plus its collaborators
  • Approve the case list
  • Generate the tests
  • Simplify over-specified mocks by hand

Validation

  • Run the suite
  • Mutation testing on the critical module
  • Confirm tests fail when you inject a bug

Security Considerations

  • Never put real credentials or customer data in fixtures

Lessons Learned

  • One class per prompt beats batching
  • Generated mocks over-specify — trim them before merge