Junit Cheatsheet

Good tests are “A TRIP”

Aha… what kind of trip?
  • Automatic. Invoking of tests as well as checking results for PASS/FAIL should be automatic.
  • Thorough. Coverage; although bugs tend to cluster around certain regions in the code, ensure that you test all key paths and scenarios.
  • Repeatable. Tests should produce the same results each time, every time.
  • Independent/Isolated. Tests should test only one thing at a time. Multiple assertions are okay as long as they are all testing one feature/behavior. When a test fails, it should pinpoint the location of the problem. Tests should not rely on each other. No assumptions about order of test execution. Ensure ‘clean slate’ before each test by using setup/teardown appropriately.
  • Professional. In the long run you’ll have as much test code as production (if not more), therefore follow the same standard of good-design for your test code. Well factored methods-classes with intention-revealing names, no duplication, tests with good names, etc.

Given-When-Then / Arrange-Act-Assert Pattern

  @Test
  public void testMethod() {
    // Given
    // Perform the setup and initialization required for the test.
    // When
    // Take action(s) required for the test.
    // Then
    // Verify the outcome(s) of the test.
  }

Source

https://java-design-patterns.com/patterns/arrange-act-assert/

Continue reading: