Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.before.all.global;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class DatabaseSetupExtension implements BeforeAllCallback {

private static boolean initialized = false;

@Override
public void beforeAll(ExtensionContext context) throws Exception {
if (!initialized) {
initialized = true;
// Global setup: Initialize database connections
System.out.println("Initializing global database connections...");
// Example: DatabaseConnectionPool.initialize();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.before.all.global;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ExampleTest {

@BeforeAll
static void setup() {
System.out.println("ExampleTest1 - Execute: BeforeAll");
// Initialize class-specific resources
}

@Test
void test1() {
System.out.println("ExampleTest1 - Execute test 1");
// Test logic
}

@Test
void test2() {
System.out.println("ExampleTest1 - Execute test 2");
// Test logic
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.before.all.global;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ExampleTest2 {

@BeforeAll
static void setup() {
System.out.println("ExampleTest2 - Execute: BeforeAll");
// Initialize class-specific resources
}

@Test
void test1() {
System.out.println("ExampleTest2 - Execute test 1");
// Test logic
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.before.all.global;

import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;

public class GlobalDatabaseListener implements TestExecutionListener {

@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
// Global setup
System.out.println("GlobalDatabaseListener # testPlanExecutionStarted ");
// Example: DatabaseConnectionPool.initialize();
}

@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
// Global teardown
System.out.println("GlobalDatabaseListener # testPlanExecutionFinished");
// Example: DatabaseConnectionPool.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.before.all.global;

import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.LauncherSessionListener;

public class GlobalDatabaseSessionListener implements LauncherSessionListener {

@Override
public void launcherSessionOpened(LauncherSession session) {
// Global setup before session starts
System.out.println("launcherSessionOpened");
}

@Override
public void launcherSessionClosed(LauncherSession session) {
// Global teardown after session ends
System.out.println("launcherSessionClosed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.baeldung.before.all.global.DatabaseSetupExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.baeldung.before.all.global.GlobalDatabaseSessionListener
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.baeldung.before.all.global.GlobalDatabaseListener
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.extensions.autodetection.enabled = true