Skip to content

Commit 591e5c0

Browse files
committed
Introduce demos for user-supplied lifecycle callback methods
Issue: #1620
1 parent 495e7ca commit 591e5c0

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2015-2019 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example.callbacks;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
16+
/**
17+
* Abstract base class for tests that use the database.
18+
*/
19+
abstract class AbstractDatabaseTests {
20+
21+
@BeforeEach
22+
void connectToDatabase() {
23+
System.out.println(" @BeforeEach " + AbstractDatabaseTests.class.getSimpleName() + ".connectToDatabase()");
24+
}
25+
26+
@AfterEach
27+
void disconnectFromDatabase() {
28+
System.out.println(" @AfterEach " + AbstractDatabaseTests.class.getSimpleName() + ".disconnectFromDatabase()");
29+
}
30+
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2015-2019 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example.callbacks;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
18+
/**
19+
* Example of "broken" lifecycle method configuration.
20+
*
21+
* <p>Test data is inserted before the database connection has been opened.
22+
*
23+
* <p>Database connection is closed before deleting test data.
24+
*/
25+
@ExtendWith({ Extension1.class, Extension2.class })
26+
class BrokenLifecycleMethodConfigDemo {
27+
28+
@BeforeEach
29+
void connectToDatabase() {
30+
System.out.println(" @BeforeEach " + getClass().getSimpleName() + ".connectToDatabase()");
31+
}
32+
33+
@BeforeEach
34+
void insertTestDataIntoDatabase() {
35+
System.out.println(" @BeforeEach " + getClass().getSimpleName() + ".insertTestDataIntoDatabase()");
36+
}
37+
38+
@Test
39+
void testDatabaseFunctionality() {
40+
System.out.println(" @Test " + getClass().getSimpleName() + ".testDatabaseFunctionality()");
41+
}
42+
43+
@AfterEach
44+
void deleteTestDataInDatabase() {
45+
System.out.println(" @AfterEach " + getClass().getSimpleName() + ".deleteTestDataInDatabase()");
46+
}
47+
48+
@AfterEach
49+
void disconnectFromDatabase() {
50+
System.out.println(" @AfterEach " + getClass().getSimpleName() + ".disconnectFromDatabase()");
51+
}
52+
53+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2015-2019 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example.callbacks;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
18+
/**
19+
* Extension of {@link AbstractDatabaseTests} that inserts test data
20+
* into the database (after the database connection has been opened)
21+
* and deletes test data (before the database connection is closed).
22+
*/
23+
@ExtendWith({ Extension1.class, Extension2.class })
24+
class DatabaseTestsDemo extends AbstractDatabaseTests {
25+
26+
@BeforeEach
27+
void insertTestDataIntoDatabase() {
28+
System.out.println(" @BeforeEach " + getClass().getSimpleName() + ".insertTestDataIntoDatabase()");
29+
}
30+
31+
@Test
32+
void testDatabaseFunctionality() {
33+
System.out.println(" @Test " + getClass().getSimpleName() + ".testDatabaseFunctionality()");
34+
}
35+
36+
@AfterEach
37+
void deleteTestDataInDatabase() {
38+
System.out.println(" @AfterEach " + getClass().getSimpleName() + ".deleteTestDataInDatabase()");
39+
}
40+
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2015-2019 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example.callbacks;
12+
13+
import org.junit.jupiter.api.extension.AfterEachCallback;
14+
import org.junit.jupiter.api.extension.BeforeEachCallback;
15+
import org.junit.jupiter.api.extension.ExtensionContext;
16+
17+
public class Extension1 implements BeforeEachCallback, AfterEachCallback {
18+
19+
@Override
20+
public void beforeEach(ExtensionContext context) {
21+
System.out.println(getClass().getSimpleName() + ".beforeEach()");
22+
}
23+
24+
@Override
25+
public void afterEach(ExtensionContext context) {
26+
System.out.println(getClass().getSimpleName() + ".afterEach()");
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2015-2019 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example.callbacks;
12+
13+
import org.junit.jupiter.api.extension.AfterEachCallback;
14+
import org.junit.jupiter.api.extension.BeforeEachCallback;
15+
import org.junit.jupiter.api.extension.ExtensionContext;
16+
17+
public class Extension2 implements BeforeEachCallback, AfterEachCallback {
18+
19+
@Override
20+
public void beforeEach(ExtensionContext context) {
21+
System.out.println(getClass().getSimpleName() + ".beforeEach()");
22+
}
23+
24+
@Override
25+
public void afterEach(ExtensionContext context) {
26+
System.out.println(getClass().getSimpleName() + ".afterEach()");
27+
}
28+
29+
}

0 commit comments

Comments
 (0)