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:
@@ -0,0 +1,12 @@
|
||||
new Sandwich()
|
||||
@BeforeEach
|
||||
@Test second
|
||||
@AfterEach
|
||||
new Sandwich()
|
||||
@BeforeEach
|
||||
@Test first
|
||||
@AfterEach
|
||||
-- results --
|
||||
SUCCESSFUL second()
|
||||
SUCCESSFUL first()
|
||||
SUCCESSFUL Fixtures$Sandwich
|
||||
@@ -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
|
||||
@@ -0,0 +1,10 @@
|
||||
Grandparent @BeforeEach
|
||||
Parent @BeforeEach
|
||||
Child @BeforeEach
|
||||
Child @Test
|
||||
Child @AfterEach
|
||||
Parent @AfterEach
|
||||
Grandparent @AfterEach
|
||||
-- results --
|
||||
SUCCESSFUL test()
|
||||
SUCCESSFUL Fixtures$Child
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,7 @@
|
||||
middle
|
||||
zeta
|
||||
alpha
|
||||
@Test
|
||||
-- results --
|
||||
SUCCESSFUL test()
|
||||
SUCCESSFUL Fixtures$TwoBeforeEach
|
||||
@@ -0,0 +1,5 @@
|
||||
@BeforeEach sees myTest() / myTest
|
||||
@Test myTest
|
||||
-- results --
|
||||
SUCCESSFUL myTest()
|
||||
SUCCESSFUL Fixtures$InjectsTestInfo
|
||||
@@ -0,0 +1,9 @@
|
||||
new PerClass()
|
||||
@BeforeEach
|
||||
a counter=1
|
||||
@BeforeEach
|
||||
b counter=2
|
||||
-- results --
|
||||
SUCCESSFUL a()
|
||||
SUCCESSFUL b()
|
||||
SUCCESSFUL Fixtures$PerClass
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,2 @@
|
||||
LOG: Setting default API mock behavior...
|
||||
Running: testServiceWithActiveApi()
|
||||
@@ -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!
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user