hanki.dev

Use functions in assert checks in Junit tests

Today I learned that you can use functions in assert checks. For example, instead of checking multiple values of List

assertEquals("AAA", options.get(0));
assertEquals("BBB", options.get(1));
assertEquals("CCC", options.get(2));

You can create checkAlphabetical function with a for loop.

// Define function
private boolean checkAlphabetical(List<String> options) {
   // Magic code 🧙‍♂️
   // return false if some check fails
   // otherwise return true
}

// Call the function inside a test
assert checkAlphabetical(options);

#java