Junit 5 Examples

Published: 2020-05-14, Updated: 2023-03-11


Check if exception was thrown, then check the message

import static org.junit.jupiter.api.Assertions.assertThrows;

void exceptionTesting() {
    MyException thrown = assertThrows(
           () -> myObject.doThing(),
           "Expected doThing() to throw, but it didn't"


Save state between tests executions

public void beforeAll(ExtensionContext context) throws Exception {
  final Store store = context

  if (store.get(EmbeddedPostgres.class.getName(), EmbeddedPostgres.class) == null) {
    store.put(EmbeddedPostgres.class.getName(), new EmbeddedPostgres())

TemporaryDirectory no Junit5

void mustCreatePostUsingFormPage(@TempDir Path tempDir) {
  final var tmpPhoto = Files.createTempFile(tempDir, "img", "png");

awesome junit 5, junit 5 commands, junit commands, junit bookmarks

Run lua on top of JavaScript The Clean Code