diff --git a/pom.xml b/pom.xml index 9298c8ad17..22964250de 100644 --- a/pom.xml +++ b/pom.xml @@ -124,11 +124,6 @@ - - org.junit.platform - junit-platform-surefire-provider - ${junit-platform.version} - org.junit.jupiter junit-jupiter-engine @@ -1527,7 +1522,7 @@ 1.1.7 - 2.21.0 + 2.22.0 3.7.0 1.6.0 1.8 @@ -1549,8 +1544,8 @@ 1.2 2.9.8 1.3 - 1.2.0 - 5.2.0 + 1.4.2 + 5.4.2 0.3.1 2.5.1 0.0.1 diff --git a/testing-modules/junit-5/pom.xml b/testing-modules/junit-5/pom.xml index 84e0577cfe..b3074635a7 100644 --- a/testing-modules/junit-5/pom.xml +++ b/testing-modules/junit-5/pom.xml @@ -139,7 +139,7 @@ 5.4.2 2.8.2 2.0.0-RC.1 - 2.21.0 + 2.22.0 1.6.0 5.0.1.RELEASE diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/MultipleExtensionsUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/MultipleExtensionsUnitTest.java new file mode 100644 index 0000000000..db37e9a6d2 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/MultipleExtensionsUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.extension.RegisterExtension; +import com.baeldung.extensions.EmployeeDatabaseSetupExtension; + +public class MultipleExtensionsUnitTest { + + @Order(1) + @RegisterExtension + static EmployeeDatabaseSetupExtension SECOND_DB = + new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbTwo;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", ""); + + @Order(0) + @RegisterExtension + static EmployeeDatabaseSetupExtension FIRST_DB = + new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbOne;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", ""); + + @RegisterExtension + static EmployeeDatabaseSetupExtension LAST_DB = + new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbLast;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", ""); + + @Test + public void justDemonstratingTheIdea() { + // empty test + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/ProgrammaticEmployeesUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/ProgrammaticEmployeesUnitTest.java new file mode 100644 index 0000000000..a29fafd193 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/ProgrammaticEmployeesUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung; + +import java.sql.SQLException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.baeldung.helpers.Employee; +import com.baeldung.extensions.EmployeeDaoParameterResolver; +import com.baeldung.extensions.EmployeeDatabaseSetupExtension; +import com.baeldung.extensions.EnvironmentExtension; +import com.baeldung.helpers.EmployeeJdbcDao; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith({ EnvironmentExtension.class, EmployeeDaoParameterResolver.class }) +public class ProgrammaticEmployeesUnitTest { + + private EmployeeJdbcDao employeeDao; + + @RegisterExtension static EmployeeDatabaseSetupExtension DB = + new EmployeeDatabaseSetupExtension("jdbc:h2:mem:AnotherDb;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", ""); + + public ProgrammaticEmployeesUnitTest(EmployeeJdbcDao employeeDao) { + this.employeeDao = employeeDao; + } + + @Test + public void whenAddEmployee_thenGetEmployee() throws SQLException { + Employee emp = new Employee(1, "john"); + employeeDao.add(emp); + assertEquals(1, employeeDao.findAll().size()); + } + + @Test + public void whenGetEmployees_thenEmptyList() throws SQLException { + assertEquals(0, employeeDao.findAll().size()); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/extensions/EmployeeDatabaseSetupExtension.java b/testing-modules/junit-5/src/test/java/com/baeldung/extensions/EmployeeDatabaseSetupExtension.java index 69e420b28a..f124703690 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/extensions/EmployeeDatabaseSetupExtension.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/extensions/EmployeeDatabaseSetupExtension.java @@ -15,10 +15,20 @@ import com.baeldung.helpers.JdbcConnectionUtil; public class EmployeeDatabaseSetupExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback { - private Connection con = JdbcConnectionUtil.getConnection(); - private EmployeeJdbcDao employeeDao = new EmployeeJdbcDao(con); + private Connection con; + private EmployeeJdbcDao employeeDao; private Savepoint savepoint; + public EmployeeDatabaseSetupExtension() { + con = JdbcConnectionUtil.getConnection(); + employeeDao = new EmployeeJdbcDao(con); + } + + public EmployeeDatabaseSetupExtension(String jdbcUrl, String driver, String username, String password) { + con = JdbcConnectionUtil.getConnection(jdbcUrl, driver, username, password); + employeeDao = new EmployeeJdbcDao(con); + } + @Override public void afterAll(ExtensionContext context) throws SQLException { if (con != null) { diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/helpers/EmployeeJdbcDao.java b/testing-modules/junit-5/src/test/java/com/baeldung/helpers/EmployeeJdbcDao.java index 7600f763cd..4b274cff39 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/helpers/EmployeeJdbcDao.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/helpers/EmployeeJdbcDao.java @@ -19,7 +19,7 @@ public class EmployeeJdbcDao { } public void createTable() throws SQLException { - String createQuery = "CREATE TABLE employees(id long primary key, firstName varchar(50))"; + String createQuery = "CREATE TABLE IF NOT EXISTS employees(id long primary key, firstName varchar(50))"; PreparedStatement pstmt = con.prepareStatement(createQuery); pstmt.execute(); diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/helpers/JdbcConnectionUtil.java b/testing-modules/junit-5/src/test/java/com/baeldung/helpers/JdbcConnectionUtil.java index f380f9674c..ccba627234 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/helpers/JdbcConnectionUtil.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/helpers/JdbcConnectionUtil.java @@ -11,22 +11,49 @@ public class JdbcConnectionUtil { private static Connection con; public static Connection getConnection() { - if (con == null) { + if (con == null || isClosed(con)) { try { Properties props = new Properties(); props.load(JdbcConnectionUtil.class.getResourceAsStream("jdbc.properties")); - Class.forName(props.getProperty("jdbc.driver")); - con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password")); + + String jdbcUrl = props.getProperty("jdbc.url"); + String driver = props.getProperty("jdbc.driver"); + String username = props.getProperty("jdbc.user"); + String password = props.getProperty("jdbc.password"); + con = getConnection(jdbcUrl, driver, username, password); return con; } catch (IOException exc) { exc.printStackTrace(); + } + + return null; + } + return con; + } + + public static Connection getConnection(String jdbcUrl, String driver, String username, String password) { + if (con == null || isClosed(con)) { + try { + Class.forName(driver); + con = DriverManager.getConnection(jdbcUrl, username, password); + return con; } catch (ClassNotFoundException exc) { exc.printStackTrace(); } catch (SQLException exc) { exc.printStackTrace(); } + return null; } + return con; } + + private static boolean isClosed(Connection con) { + try { + return con.isClosed(); + } catch (SQLException e) { + return true; + } + } } diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java index d1d08c6e62..d225e4547e 100644 --- a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java @@ -34,7 +34,7 @@ public class AssertionUnitTest { "heading", () -> assertEquals(4, 2 * 2, "4 is 2 times 2"), () -> assertEquals("java", "JAVA".toLowerCase()), - () -> assertEquals(null, null, "null is equal to null") + () -> assertEquals((String) null, (String) null, "null is equal to null") ); } }