Use assertj instead of native junit assertions because it brings more context in the error messages.
You can use InvocationInterceptor interface to create a junit extension.
public class TimeExtension implements InvocationInterceptor { @Override public void interceptTestMethod(Invocation invocation, ReflectiveInvocation ref) { try (...) { invocation.proceed(); } }}// Then, in your tests@RegisterExtensionTimeExtension timeExtension = new TimeExtension("2023-12-25");
⚠️ This is not thread safe!
You can use assert softly to assert all assertions in one go. You can also inject SoftAssertions so you don’t need to create it for all tests:
Parameterized tests are powerful to reduce boilerplates. BUT do not abuse them, i.e. makes multiple argument. You can mitigate by create a class containing all the arguments in the test.
Some best practices for parameterized tests:
tiny, safe baby-steps refactoring with IDE, pairing or mobbing
extract and test code with a clear role = maintainable tests
characterization tests: capture input/output as a black-box
Gherkin Language to express business logic by non-technical people.
Use cucumber testing tool to use this feature file.
Developer’s dream: business people will maintain the .feature files. But it never works.
It could work if the dev works pair on them (e.g. after an event storming).
When should you use a .feature?