junit 5 extensions (#2233)
* junit 5 extensions * fix exceptions * add back parent
This commit is contained in:
parent
e6a0bbe090
commit
dfae14537b
|
@ -9,7 +9,7 @@
|
|||
|
||||
<name>junit5</name>
|
||||
<description>Intro to JUnit 5</description>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
@ -19,16 +19,24 @@
|
|||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
|
||||
<junit.platform.version>1.0.0-M4</junit.platform.version>
|
||||
<junit.vintage.version>4.12.0-M4</junit.vintage.version>
|
||||
<junit4.version>4.12</junit4.version>
|
||||
<junit.jupiter.version>5.0.0-M5</junit.jupiter.version>
|
||||
<junit.platform.version>1.0.0-M5</junit.platform.version>
|
||||
<junit.vintage.version>4.12.0-M5</junit.vintage.version>
|
||||
<log4j2.version>2.8.2</log4j2.version>
|
||||
<h2.version>1.4.196</h2.version>
|
||||
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<junit4.version>4.12</junit4.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/test/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
@ -49,44 +57,60 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.TestLauncher</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit4.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit.vintage.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency> -->
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit.vintage.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit4.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import com.baeldung.extensions.EmployeeDaoParameterResolver;
|
||||
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
|
||||
import com.baeldung.extensions.EnvironmentExtension;
|
||||
import com.baeldung.extensions.IgnoreFileNotFoundExceptionExtension;
|
||||
import com.baeldung.extensions.LoggingExtension;
|
||||
import com.baeldung.helpers.Employee;
|
||||
import com.baeldung.helpers.EmployeeJdbcDao;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@ExtendWith({ EnvironmentExtension.class, EmployeeDatabaseSetupExtension.class, EmployeeDaoParameterResolver.class })
|
||||
@ExtendWith(LoggingExtension.class)
|
||||
@ExtendWith(IgnoreFileNotFoundExceptionExtension.class)
|
||||
public class EmployeesTest {
|
||||
|
||||
private EmployeeJdbcDao employeeDao;
|
||||
|
||||
private Logger logger;
|
||||
|
||||
public EmployeesTest(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());
|
||||
}
|
||||
|
||||
public void setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.junit.platform.launcher.LauncherDiscoveryRequest;
|
||||
import org.junit.platform.launcher.TestExecutionListener;
|
||||
import org.junit.platform.launcher.TestPlan;
|
||||
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
|
||||
import org.junit.platform.launcher.core.LauncherFactory;
|
||||
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
|
||||
|
||||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import org.junit.platform.launcher.Launcher;
|
||||
|
||||
public class TestLauncher {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//@formatter:off
|
||||
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
|
||||
.selectors(selectClass("com.baeldung.EmployeesTest"))
|
||||
.configurationParameter("junit.conditions.deactivate", "com.baeldung.extensions.*")
|
||||
.configurationParameter("junit.extensions.autodetection.enabled", "true")
|
||||
.build();
|
||||
|
||||
//@formatter:on
|
||||
|
||||
TestPlan plan = LauncherFactory.create().discover(request);
|
||||
Launcher launcher = LauncherFactory.create();
|
||||
SummaryGeneratingListener summaryGeneratingListener = new SummaryGeneratingListener();
|
||||
launcher.execute(request, new TestExecutionListener[] { summaryGeneratingListener });
|
||||
launcher.execute(request);
|
||||
|
||||
summaryGeneratingListener.getSummary().printTo(new PrintWriter(System.out));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.extensions;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
|
||||
import com.baeldung.helpers.EmployeeJdbcDao;
|
||||
import com.baeldung.helpers.JdbcConnectionUtil;
|
||||
|
||||
public class EmployeeDaoParameterResolver implements ParameterResolver {
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return parameterContext.getParameter().getType().equals(EmployeeJdbcDao.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return new EmployeeJdbcDao(JdbcConnectionUtil.getConnection());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.extensions;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Savepoint;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
import com.baeldung.helpers.EmployeeJdbcDao;
|
||||
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 Savepoint savepoint;
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext context) throws SQLException {
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeAll(ExtensionContext context) throws SQLException {
|
||||
employeeDao.createTable();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(ExtensionContext context) throws SQLException {
|
||||
con.rollback(savepoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) throws SQLException {
|
||||
con.setAutoCommit(false);
|
||||
savepoint = con.setSavepoint("before");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.extensions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
||||
import org.junit.jupiter.api.extension.ExecutionCondition;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
public class EnvironmentExtension implements ExecutionCondition {
|
||||
|
||||
@Override
|
||||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
||||
Properties props = new Properties();
|
||||
|
||||
try {
|
||||
props.load(EnvironmentExtension.class.getResourceAsStream("application.properties"));
|
||||
String env = props.getProperty("env");
|
||||
if ("qa".equalsIgnoreCase(env)) {
|
||||
return ConditionEvaluationResult.disabled("Test disabled on QA environment");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ConditionEvaluationResult.enabled("Test enabled on QA environment");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.extensions;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
|
||||
|
||||
public class IgnoreFileNotFoundExceptionExtension implements TestExecutionExceptionHandler {
|
||||
|
||||
Logger logger = LogManager.getLogger(IgnoreFileNotFoundExceptionExtension.class);
|
||||
|
||||
@Override
|
||||
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
|
||||
|
||||
if (throwable instanceof FileNotFoundException) {
|
||||
logger.error("File not found:" + throwable.getMessage());
|
||||
return;
|
||||
}
|
||||
throw throwable;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.extensions;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
|
||||
|
||||
public class LoggingExtension implements TestInstancePostProcessor {
|
||||
|
||||
@Override
|
||||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
|
||||
Logger logger = LogManager.getLogger(testInstance.getClass());
|
||||
testInstance.getClass().getMethod("setLogger", Logger.class).invoke(testInstance, logger);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.helpers;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class EmployeeJdbcDao {
|
||||
|
||||
private Connection con;
|
||||
|
||||
public EmployeeJdbcDao(Connection con) {
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
public void createTable() throws SQLException {
|
||||
String createQuery = "CREATE TABLE employees(id long primary key, firstName varchar(50))";
|
||||
PreparedStatement pstmt = con.prepareStatement(createQuery);
|
||||
|
||||
pstmt.execute();
|
||||
}
|
||||
|
||||
public void add(Employee emp) throws SQLException {
|
||||
String insertQuery = "INSERT INTO employees(id, firstName) VALUES(?,?)";
|
||||
PreparedStatement pstmt = con.prepareStatement(insertQuery);
|
||||
pstmt.setLong(1, emp.getId());
|
||||
pstmt.setString(2, emp.getFirstName());
|
||||
|
||||
pstmt.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
public List<Employee> findAll() throws SQLException {
|
||||
List<Employee> employees = new ArrayList<>();
|
||||
String query = "SELECT * FROM employees";
|
||||
PreparedStatement pstmt = con.prepareStatement(query);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
Employee emp = new Employee(rs.getLong("id"), rs.getString("firstName"));
|
||||
employees.add(emp);
|
||||
}
|
||||
|
||||
return employees;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.helpers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class JdbcConnectionUtil {
|
||||
|
||||
private static Connection con;
|
||||
|
||||
public static Connection getConnection() {
|
||||
if (con == null) {
|
||||
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"));
|
||||
return con;
|
||||
} catch (IOException exc) {
|
||||
exc.printStackTrace();
|
||||
} catch (ClassNotFoundException exc) {
|
||||
exc.printStackTrace();
|
||||
} catch (SQLException exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return con;
|
||||
}
|
||||
}
|
|
@ -2,17 +2,17 @@ package com.baeldung.migration.junit5.extensions;
|
|||
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.TestExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
public class TraceUnitExtension implements AfterEachCallback, BeforeEachCallback {
|
||||
|
||||
@Override
|
||||
public void beforeEach(TestExtensionContext context) throws Exception {
|
||||
public void beforeEach(ExtensionContext context) throws Exception {
|
||||
System.out.println("Starting test ... " + context.getDisplayName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(TestExtensionContext context) throws Exception {
|
||||
public void afterEach(ExtensionContext context) throws Exception {
|
||||
System.out.println("... test finished. " + context.getDisplayName());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
package com.baeldung.suites;
|
||||
|
||||
//@RunWith(JUnitPlatform.class)
|
||||
//@SelectPackages("com.baeldung")
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.platform.suite.api.SelectPackages;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@SelectPackages("com.baeldung")
|
||||
//@SelectClasses({AssertionTest.class, AssumptionTest.class, ExceptionTest.class})
|
||||
public class AllTests {
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
com.baeldung.extensions.EmployeeDaoParameterResolver
|
|
@ -0,0 +1 @@
|
|||
env=dev
|
|
@ -0,0 +1,5 @@
|
|||
#h2 db
|
||||
jdbc.driver=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
|
||||
jdbc.user=sa
|
||||
jdbc.password=
|
Loading…
Reference in New Issue