Add lifecycle module: @BeforeAll/@AfterAll demos on JUnit 6.1.3

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KqJyCidz3ZgRyHABv2GVJh
This commit is contained in:
2026-09-30 19:02:06 +00:00
co-authored by Claude Sonnet 5.5
parent 5ed079dff3
commit cfc6c79fc3
22 changed files with 689 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
@BeforeAll static (no instance yet)
a() instance=342198178
b() instance=1988644427
@AfterAll static
-- results --
SUCCESSFUL a()
SUCCESSFUL b()
SUCCESSFUL Fixtures$StaticHooks
+8
View File
@@ -0,0 +1,8 @@
@BeforeAll instance, counter=100
a() instance=18179709 counter=101
b() instance=18179709 counter=102
@AfterAll instance, counter=102
-- results --
SUCCESSFUL a()
SUCCESSFUL b()
SUCCESSFUL Fixtures$PerClassHooks
@@ -0,0 +1,6 @@
-- results --
FAILED <engine> DiscoveryIssueException: TestEngine with ID 'junit-jupiter' encountered a critical issue during test discovery:
(1) [ERROR] @BeforeAll method 'void com.ankurm.tutorials.junit.lifecycle.Fixtures$NonStaticWithoutPerClass.setupAll()' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).
Source: MethodSource [className = 'com.ankurm.tutorials.junit.lifecycle.Fixtures$NonStaticWithoutPerClass', methodName = 'setupAll', methodParameterTypes = '']
at com.ankurm.tutorials.junit.lifecycle.Fixtures$NonStaticWithoutPerClass.setupAll(SourceFile:0)
+4
View File
@@ -0,0 +1,4 @@
@BeforeAll start: about to fail
@AfterAll stop: resource is null
-- results --
FAILED Fixtures$BeforeAllFails IllegalStateException: container did not start
@@ -0,0 +1,4 @@
@AfterAll stop (unguarded)
-- results --
FAILED Fixtures$BeforeAllFailsUnguarded IllegalStateException: container did not start
suppressed NullPointerException: Cannot invoke "java.lang.AutoCloseable.close()" because "com.ankurm.tutorials.junit.lifecycle.Fixtures$BeforeAllFailsUnguarded.resource" is null
+6
View File
@@ -0,0 +1,6 @@
a() ran
@AfterAll closeDatabase
@AfterAll deleteFiles
-- results --
SUCCESSFUL a()
FAILED Fixtures$AfterAllThrows IllegalStateException: db close failed
+8
View File
@@ -0,0 +1,8 @@
Base @BeforeAll
Sub @BeforeAll
Sub a()
Sub @AfterAll
Base @AfterAll
-- results --
SUCCESSFUL a()
SUCCESSFUL Fixtures$Sub
+6
View File
@@ -0,0 +1,6 @@
HidingBase.init
HidingSub.init
HidingSub a()
-- results --
SUCCESSFUL a()
SUCCESSFUL Fixtures$HidingSub
+7
View File
@@ -0,0 +1,7 @@
second
first
third
a()
-- results --
SUCCESSFUL a()
SUCCESSFUL Fixtures$TwoBeforeAllInOneClass
+11
View File
@@ -0,0 +1,11 @@
Outer @BeforeAll
outer test
Inner static @BeforeAll
inner test
Inner static @AfterAll
Outer @AfterAll
-- results --
SUCCESSFUL outerTest()
SUCCESSFUL innerTest()
SUCCESSFUL Inner
SUCCESSFUL Fixtures$OuterWithNested
+8
View File
@@ -0,0 +1,8 @@
@BeforeAll seeded junit-<n>
a() reads: seeded once
b() reads: seeded once
@AfterAll dir still exists: true
-- results --
SUCCESSFUL a()
SUCCESSFUL b()
SUCCESSFUL Fixtures$SharedTempDir
+9
View File
@@ -0,0 +1,9 @@
server STARTED
UsesServerA test
UsesServerB test
server CLOSED
-- results --
SUCCESSFUL a()
SUCCESSFUL Fixtures$UsesServerA
SUCCESSFUL b()
SUCCESSFUL Fixtures$UsesServerB
@@ -0,0 +1,5 @@
setupAll ran
test ran
-- results --
SUCCESSFUL a()
SUCCESSFUL Fixtures$NonStaticWithoutPerClass