Skip to content

Commit 84f0795

Browse files
authored
Merge pull request eugenp#6927 from alimate/BAEL-2916
Bael 2916: Ordering Extension Registrations
2 parents 7a7b07f + 0d46cd9 commit 84f0795

File tree

8 files changed

+116
-16
lines changed

8 files changed

+116
-16
lines changed

pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@
124124
</excludes>
125125
</configuration>
126126
<dependencies>
127-
<dependency>
128-
<groupId>org.junit.platform</groupId>
129-
<artifactId>junit-platform-surefire-provider</artifactId>
130-
<version>${junit-platform.version}</version>
131-
</dependency>
132127
<dependency>
133128
<groupId>org.junit.jupiter</groupId>
134129
<artifactId>junit-jupiter-engine</artifactId>
@@ -1527,7 +1522,7 @@
15271522
<logback.version>1.1.7</logback.version>
15281523

15291524
<!-- plugins -->
1530-
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
1525+
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
15311526
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
15321527
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
15331528
<java.version>1.8</java.version>
@@ -1549,8 +1544,8 @@
15491544
<jstl.version>1.2</jstl.version>
15501545
<jackson.version>2.9.8</jackson.version>
15511546
<commons-fileupload.version>1.3</commons-fileupload.version>
1552-
<junit-platform.version>1.2.0</junit-platform.version>
1553-
<junit-jupiter.version>5.2.0</junit-jupiter.version>
1547+
<junit-platform.version>1.4.2</junit-platform.version>
1548+
<junit-jupiter.version>5.4.2</junit-jupiter.version>
15541549
<directory-maven-plugin.version>0.3.1</directory-maven-plugin.version>
15551550
<maven-install-plugin.version>2.5.1</maven-install-plugin.version>
15561551
<custom-pmd.version>0.0.1</custom-pmd.version>

testing-modules/junit-5/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
<junit.vintage.version>5.4.2</junit.vintage.version>
140140
<log4j2.version>2.8.2</log4j2.version>
141141
<powermock.version>2.0.0-RC.1</powermock.version>
142-
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
142+
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
143143
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
144144
<spring.version>5.0.1.RELEASE</spring.version>
145145
</properties>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.Order;
5+
import org.junit.jupiter.api.extension.RegisterExtension;
6+
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
7+
8+
public class MultipleExtensionsUnitTest {
9+
10+
@Order(1)
11+
@RegisterExtension
12+
static EmployeeDatabaseSetupExtension SECOND_DB =
13+
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbTwo;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
14+
15+
@Order(0)
16+
@RegisterExtension
17+
static EmployeeDatabaseSetupExtension FIRST_DB =
18+
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbOne;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
19+
20+
@RegisterExtension
21+
static EmployeeDatabaseSetupExtension LAST_DB =
22+
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbLast;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
23+
24+
@Test
25+
public void justDemonstratingTheIdea() {
26+
// empty test
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung;
2+
3+
import java.sql.SQLException;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.junit.jupiter.api.extension.RegisterExtension;
8+
9+
import com.baeldung.helpers.Employee;
10+
import com.baeldung.extensions.EmployeeDaoParameterResolver;
11+
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
12+
import com.baeldung.extensions.EnvironmentExtension;
13+
import com.baeldung.helpers.EmployeeJdbcDao;
14+
15+
import static org.junit.jupiter.api.Assertions.*;
16+
17+
@ExtendWith({ EnvironmentExtension.class, EmployeeDaoParameterResolver.class })
18+
public class ProgrammaticEmployeesUnitTest {
19+
20+
private EmployeeJdbcDao employeeDao;
21+
22+
@RegisterExtension static EmployeeDatabaseSetupExtension DB =
23+
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:AnotherDb;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
24+
25+
public ProgrammaticEmployeesUnitTest(EmployeeJdbcDao employeeDao) {
26+
this.employeeDao = employeeDao;
27+
}
28+
29+
@Test
30+
public void whenAddEmployee_thenGetEmployee() throws SQLException {
31+
Employee emp = new Employee(1, "john");
32+
employeeDao.add(emp);
33+
assertEquals(1, employeeDao.findAll().size());
34+
}
35+
36+
@Test
37+
public void whenGetEmployees_thenEmptyList() throws SQLException {
38+
assertEquals(0, employeeDao.findAll().size());
39+
}
40+
}

testing-modules/junit-5/src/test/java/com/baeldung/extensions/EmployeeDatabaseSetupExtension.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515

1616
public class EmployeeDatabaseSetupExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
1717

18-
private Connection con = JdbcConnectionUtil.getConnection();
19-
private EmployeeJdbcDao employeeDao = new EmployeeJdbcDao(con);
18+
private Connection con;
19+
private EmployeeJdbcDao employeeDao;
2020
private Savepoint savepoint;
2121

22+
public EmployeeDatabaseSetupExtension() {
23+
con = JdbcConnectionUtil.getConnection();
24+
employeeDao = new EmployeeJdbcDao(con);
25+
}
26+
27+
public EmployeeDatabaseSetupExtension(String jdbcUrl, String driver, String username, String password) {
28+
con = JdbcConnectionUtil.getConnection(jdbcUrl, driver, username, password);
29+
employeeDao = new EmployeeJdbcDao(con);
30+
}
31+
2232
@Override
2333
public void afterAll(ExtensionContext context) throws SQLException {
2434
if (con != null) {

testing-modules/junit-5/src/test/java/com/baeldung/helpers/EmployeeJdbcDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public EmployeeJdbcDao(Connection con) {
1919
}
2020

2121
public void createTable() throws SQLException {
22-
String createQuery = "CREATE TABLE employees(id long primary key, firstName varchar(50))";
22+
String createQuery = "CREATE TABLE IF NOT EXISTS employees(id long primary key, firstName varchar(50))";
2323
PreparedStatement pstmt = con.prepareStatement(createQuery);
2424

2525
pstmt.execute();

testing-modules/junit-5/src/test/java/com/baeldung/helpers/JdbcConnectionUtil.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,49 @@ public class JdbcConnectionUtil {
1111
private static Connection con;
1212

1313
public static Connection getConnection() {
14-
if (con == null) {
14+
if (con == null || isClosed(con)) {
1515
try {
1616
Properties props = new Properties();
1717
props.load(JdbcConnectionUtil.class.getResourceAsStream("jdbc.properties"));
18-
Class.forName(props.getProperty("jdbc.driver"));
19-
con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password"));
18+
19+
String jdbcUrl = props.getProperty("jdbc.url");
20+
String driver = props.getProperty("jdbc.driver");
21+
String username = props.getProperty("jdbc.user");
22+
String password = props.getProperty("jdbc.password");
23+
con = getConnection(jdbcUrl, driver, username, password);
2024
return con;
2125
} catch (IOException exc) {
2226
exc.printStackTrace();
27+
}
28+
29+
return null;
30+
}
31+
return con;
32+
}
33+
34+
public static Connection getConnection(String jdbcUrl, String driver, String username, String password) {
35+
if (con == null || isClosed(con)) {
36+
try {
37+
Class.forName(driver);
38+
con = DriverManager.getConnection(jdbcUrl, username, password);
39+
return con;
2340
} catch (ClassNotFoundException exc) {
2441
exc.printStackTrace();
2542
} catch (SQLException exc) {
2643
exc.printStackTrace();
2744
}
45+
2846
return null;
2947
}
48+
3049
return con;
3150
}
51+
52+
private static boolean isClosed(Connection con) {
53+
try {
54+
return con.isClosed();
55+
} catch (SQLException e) {
56+
return true;
57+
}
58+
}
3259
}

testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void givenMultipleAssertion_whenAssertingAll_thenOK() {
3434
"heading",
3535
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
3636
() -> assertEquals("java", "JAVA".toLowerCase()),
37-
() -> assertEquals(null, null, "null is equal to null")
37+
() -> assertEquals((String) null, (String) null, "null is equal to null")
3838
);
3939
}
4040
}

0 commit comments

Comments
 (0)