Add beforeeach module: @BeforeEach/@AfterEach lifecycle demos on JUnit 6.1.3 (JDK 25)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6FRR2Dk1163xJ5CdV
This commit is contained in:
2026-11-15 01:14:28 +05:30
co-authored by Claude Sonnet 5.5
parent bfc67986bc
commit e49be6d633
36 changed files with 690 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
new Sandwich()
@BeforeEach
@Test second
@AfterEach
new Sandwich()
@BeforeEach
@Test first
@AfterEach
-- results --
SUCCESSFUL second()
SUCCESSFUL first()
SUCCESSFUL Fixtures$Sandwich
+6
View File
@@ -0,0 +1,6 @@
addOne size=1
startsEmpty size=0
-- results --
SUCCESSFUL addOne()
SUCCESSFUL startsEmpty()
SUCCESSFUL Fixtures$FreshCart
@@ -0,0 +1,6 @@
a counter=1
b counter=1
-- results --
SUCCESSFUL a()
SUCCESSFUL b()
SUCCESSFUL Fixtures$FieldsDoNotLeak
+10
View File
@@ -0,0 +1,10 @@
Grandparent @BeforeEach
Parent @BeforeEach
Child @BeforeEach
Child @Test
Child @AfterEach
Parent @AfterEach
Grandparent @AfterEach
-- results --
SUCCESSFUL test()
SUCCESSFUL Fixtures$Child
+5
View File
@@ -0,0 +1,5 @@
OverrideSub.init
OverrideSub @Test
-- results --
SUCCESSFUL test()
SUCCESSFUL Fixtures$OverrideSub
@@ -0,0 +1,4 @@
OverrideNoAnnotationSub @Test
-- results --
SUCCESSFUL test()
SUCCESSFUL Fixtures$OverrideNoAnnotationSub
+6
View File
@@ -0,0 +1,6 @@
@BeforeEach
@Test boom
@AfterEach (runs although the test failed)
-- results --
FAILED boom() AssertionFailedError: this test is designed to fail
SUCCESSFUL Fixtures$FailingTest
+10
View File
@@ -0,0 +1,10 @@
@BeforeEach (about to fail)
@AfterEach, resource is null
@BeforeEach (about to fail)
@AfterEach, resource is null
-- results --
FAILED a() IllegalStateException: setup failed
suppressed NullPointerException: Cannot invoke "java.lang.AutoCloseable.close()" because "this.resource" is null
FAILED b() IllegalStateException: setup failed
suppressed NullPointerException: Cannot invoke "java.lang.AutoCloseable.close()" because "this.resource" is null
SUCCESSFUL Fixtures$BeforeEachFails
+7
View File
@@ -0,0 +1,7 @@
middle
zeta
alpha
@Test
-- results --
SUCCESSFUL test()
SUCCESSFUL Fixtures$TwoBeforeEach
+5
View File
@@ -0,0 +1,5 @@
@BeforeEach sees myTest() / myTest
@Test myTest
-- results --
SUCCESSFUL myTest()
SUCCESSFUL Fixtures$InjectsTestInfo
+9
View File
@@ -0,0 +1,9 @@
new PerClass()
@BeforeEach
a counter=1
@BeforeEach
b counter=2
-- results --
SUCCESSFUL a()
SUCCESSFUL b()
SUCCESSFUL Fixtures$PerClass
+13
View File
@@ -0,0 +1,13 @@
Outer @BeforeEach
Outer @Test
Outer @AfterEach
Outer @BeforeEach
Inner @BeforeEach
Inner @Test
Inner @AfterEach
Outer @AfterEach
-- results --
SUCCESSFUL outerTest()
SUCCESSFUL innerTest()
SUCCESSFUL Inner
SUCCESSFUL Fixtures$Outer
@@ -0,0 +1,10 @@
BeforeEachCallback
@BeforeEach
BeforeTestExecutionCallback
@Test
AfterTestExecutionCallback
@AfterEach
AfterEachCallback
-- results --
SUCCESSFUL test()
SUCCESSFUL Fixtures$WithExtension
+6
View File
@@ -0,0 +1,6 @@
@Test
@AfterEach closeDatabase (throws)
@AfterEach deleteFiles
-- results --
FAILED test() IllegalStateException: db close failed
SUCCESSFUL Fixtures$TwoAfterEach
@@ -0,0 +1,14 @@
== StaticBeforeEach
-- results --
FAILED <engine> DiscoveryIssueException: TestEngine with ID 'junit-jupiter' encountered a critical issue during test discovery:
(1) [ERROR] @BeforeEach method 'static void com.ankurm.tutorials.junit.beforeeach.Fixtures$StaticBeforeEach.setUp()' must not be static.
Source: MethodSource [className = 'com.ankurm.tutorials.junit.beforeeach.Fixtures$StaticBeforeEach', methodName = 'setUp', methodParameterTypes = '']
at com.ankurm.tutorials.junit.beforeeach.Fixtures$StaticBeforeEach.setUp(SourceFile:0)
== NonVoidBeforeEach
-- results --
FAILED <engine> DiscoveryIssueException: TestEngine with ID 'junit-jupiter' encountered a critical issue during test discovery:
(1) [ERROR] @BeforeEach method 'java.lang.String com.ankurm.tutorials.junit.beforeeach.Fixtures$NonVoidBeforeEach.setUp()' must not return a value.
Source: MethodSource [className = 'com.ankurm.tutorials.junit.beforeeach.Fixtures$NonVoidBeforeEach', methodName = 'setUp', methodParameterTypes = '']
at com.ankurm.tutorials.junit.beforeeach.Fixtures$NonVoidBeforeEach.setUp(SourceFile:0)
@@ -0,0 +1,5 @@
private @BeforeEach ran
@Test
-- results --
SUCCESSFUL test()
SUCCESSFUL Fixtures$PrivateBeforeEach
@@ -0,0 +1,5 @@
created file
temp file exists after cleanup: false
-- results --
SUCCESSFUL writes()
SUCCESSFUL Fixtures$TempFileCleanup
+2
View File
@@ -0,0 +1,2 @@
LOG: Setting default API mock behavior...
Running: testServiceWithActiveApi()
+4
View File
@@ -0,0 +1,4 @@
Executing first test: Checking User creation
Cleaning up resources and resetting state...
Executing second test: Checking User deletion
Cleaning up resources and resetting state...
@@ -0,0 +1,4 @@
LOG: Cleaning tables and seeding User data...
Executing Test: findUserById()
LOG: Cleaning tables and seeding User data...
Executing Test: deleteUser()
@@ -0,0 +1,3 @@
Database connection established.
Testing user insertion logic...
Database connection closed.
@@ -0,0 +1 @@
Cleanup still happens, keeping the environment stable!
+1
View File
@@ -0,0 +1 @@
Deleted temporary file: test_report<random>.txt
@@ -0,0 +1,5 @@
BASE: Global Infrastructure Setup (e.g., Container Start)
CHILD: Local Test Data Seeding (e.g., User Mocking)
TEST: Execution of Business Logic Checks
CHILD: Local Test Data Cleanup
BASE: Global Infrastructure Teardown
@@ -0,0 +1,2 @@
LOG: Injecting 'ADMIN' security context...
Running: testAdminSensitiveOperation()
@@ -0,0 +1,4 @@
DEBUG: Setting up a fresh shopping cart...
DEBUG: Running testAddItem
DEBUG: Setting up a fresh shopping cart...
DEBUG: Running testCartIsInitiallyEmpty
@@ -0,0 +1,2 @@
app.ui.theme during test: DARK
app.ui.theme after cleanup: null
@@ -0,0 +1,2 @@
LOG: Created temporary directory: junit_test_9916606615363270050
Running: testFileWriteOperation()
@@ -0,0 +1 @@
INFO: Preparing environment for: cartStartsEmpty()