Library › Coding AI › Software developers

Write 3 named JUnit 5 tests with plain assertions

Practice: Setting limits and rules. About 5 minutes.

About this exercise

Write MoneyTest for Money.parse with exactly 3 @Test methods named parsesThousandsSeparator ("$1,234.50" gives 123450 cents), rejectsMissingDollarSign ("12.00" throws IllegalArgumentException with message "missing $") and rejectsThreeDecimals ("$1.999" throws with "too many decimals"). Your team uses plain JUnit 5 only: no AssertJ, Hamcrest, Mockito or parameterized tests. Exactly 1 code block of at most 26 lines, under 20 words of text.

Common mistakes when you ask AI for this

  • A big test class. The AI writes 8 to 12 tests including blank input, negatives and rounding. Try: Say: "Exactly 3 @Test methods" and name them.
  • Not checking the message. The AI often asserts only the exception type, which passes even when the wrong check fires. Try: Add: "Check the exception message: 'missing $' and 'too many decimals'."
  • Libraries you don't have. The AI likes AssertJ's assertThat, which was removed from this build. Try: Add: "Plain JUnit 5 Assertions only. No AssertJ, Hamcrest or Mockito."
  • An explanation of each test. The AI explains the tests and how to run Gradle. Try: Add: "One code block, no text."

What a strong prompt includes

  • Sets the number of tests
  • Names the tests
  • Checks the messages
  • Rules out AssertJ
  • Asks for only code

Practice this now ↑

Related exercises