Skip to content

Commit 004e230

Browse files
Bael 9404 (#18774)
* init * test updated * example code * apply suggestions and sync with draft code * initial * initial * revert * test * test * test * move to another module * one more * package name * fixes
1 parent bbae004 commit 004e230

File tree

9 files changed

+107
-0
lines changed

9 files changed

+107
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.before.all.global;
2+
3+
import org.junit.jupiter.api.extension.BeforeAllCallback;
4+
import org.junit.jupiter.api.extension.ExtensionContext;
5+
6+
public class DatabaseSetupExtension implements BeforeAllCallback {
7+
8+
private static boolean initialized = false;
9+
10+
@Override
11+
public void beforeAll(ExtensionContext context) throws Exception {
12+
if (!initialized) {
13+
initialized = true;
14+
// Global setup: Initialize database connections
15+
System.out.println("Initializing global database connections...");
16+
// Example: DatabaseConnectionPool.initialize();
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.before.all.global;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ExampleTest {
7+
8+
@BeforeAll
9+
static void setup() {
10+
System.out.println("ExampleTest1 - Execute: BeforeAll");
11+
// Initialize class-specific resources
12+
}
13+
14+
@Test
15+
void test1() {
16+
System.out.println("ExampleTest1 - Execute test 1");
17+
// Test logic
18+
}
19+
20+
@Test
21+
void test2() {
22+
System.out.println("ExampleTest1 - Execute test 2");
23+
// Test logic
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.before.all.global;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ExampleTest2 {
7+
8+
@BeforeAll
9+
static void setup() {
10+
System.out.println("ExampleTest2 - Execute: BeforeAll");
11+
// Initialize class-specific resources
12+
}
13+
14+
@Test
15+
void test1() {
16+
System.out.println("ExampleTest2 - Execute test 1");
17+
// Test logic
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.before.all.global;
2+
3+
import org.junit.platform.launcher.TestExecutionListener;
4+
import org.junit.platform.launcher.TestPlan;
5+
6+
public class GlobalDatabaseListener implements TestExecutionListener {
7+
8+
@Override
9+
public void testPlanExecutionStarted(TestPlan testPlan) {
10+
// Global setup
11+
System.out.println("GlobalDatabaseListener # testPlanExecutionStarted ");
12+
// Example: DatabaseConnectionPool.initialize();
13+
}
14+
15+
@Override
16+
public void testPlanExecutionFinished(TestPlan testPlan) {
17+
// Global teardown
18+
System.out.println("GlobalDatabaseListener # testPlanExecutionFinished");
19+
// Example: DatabaseConnectionPool.shutdown();
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.before.all.global;
2+
3+
import org.junit.platform.launcher.LauncherSession;
4+
import org.junit.platform.launcher.LauncherSessionListener;
5+
6+
public class GlobalDatabaseSessionListener implements LauncherSessionListener {
7+
8+
@Override
9+
public void launcherSessionOpened(LauncherSession session) {
10+
// Global setup before session starts
11+
System.out.println("launcherSessionOpened");
12+
}
13+
14+
@Override
15+
public void launcherSessionClosed(LauncherSession session) {
16+
// Global teardown after session ends
17+
System.out.println("launcherSessionClosed");
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.baeldung.before.all.global.DatabaseSetupExtension
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.baeldung.before.all.global.GlobalDatabaseSessionListener
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.baeldung.before.all.global.GlobalDatabaseListener
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
junit.jupiter.extensions.autodetection.enabled = true

0 commit comments

Comments
 (0)