From f0277ca4b0cafb0ef4a670b05e08ebe6319fc600 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 27 Apr 2019 22:36:54 +0530 Subject: [PATCH] BAEL2848 JUnit Tagging and Filtering Tests --- testing-modules/junit-5-2/pom.xml | 167 ++++++++++++++++++ .../baeldung/junit/tags/example/Employee.java | 44 +++++ .../junit/tags/example/EmployeeDAO.java | 66 +++++++ .../junit/tags/example/EmployeeRowMapper.java | 21 +++ .../junit/tags/example/SpringJdbcConfig.java | 20 +++ .../src/main/resources/jdbc/schema.sql | 7 + .../main/resources/jdbc/springJdbc-config.xml | 19 ++ .../src/main/resources/jdbc/test-data.sql | 7 + .../EmployeeDAOCategoryIntegrationTest.java | 67 +++++++ .../categories/EmployeeDAOUnitTestSuite.java | 12 ++ .../baeldung/categories/IntegrationTest.java | 4 + .../com/baeldung/categories/UnitTest.java | 4 + .../example/EmployeeDAOIntegrationTest.java | 42 +++++ .../junit/tags/example/EmployeeUnitTest.java | 41 +++++ .../EmployeeDAOCategoryIntegrationTest.java | 69 ++++++++ .../tags/EmployeeDAOIntegrationTest.java | 70 ++++++++ .../baeldung/tags/EmployeeDAOTestSuite.java | 12 ++ testing-modules/pom.xml | 1 + 18 files changed, 673 insertions(+) create mode 100644 testing-modules/junit-5-2/pom.xml create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java create mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql create mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml create mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java diff --git a/testing-modules/junit-5-2/pom.xml b/testing-modules/junit-5-2/pom.xml new file mode 100644 index 0000000000..ec535f8413 --- /dev/null +++ b/testing-modules/junit-5-2/pom.xml @@ -0,0 +1,167 @@ + + + 4.0.0 + + junit-5-2 + 1.0-SNAPSHOT + junit-5-2 + JUnit-5 extended + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.junit.platform + junit-platform-engine + ${junit.platform.version} + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + org.junit.jupiter + junit-jupiter-migrationsupport + ${junit.vintage.version} + test + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + + + com.h2database + h2 + ${h2.version} + + + org.springframework + spring-test + ${spring.version} + test + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + + + + + + src/test/resources + true + + + src/main/resources + true + + + + + + + filtering + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + **/*IntegrationTest.java + + + + + + + + category + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + com.baeldung.categories.UnitTest + com.baeldung.categories.IntegrationTest + + + + + + + tags + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + UnitTest + IntegrationTest + + + + + + + + + + 5.3.1 + 2.23.0 + 1.2.0 + 5.2.0 + 2.8.2 + 1.4.196 + 2.21.0 + 1.6.0 + 5.0.6.RELEASE + + + diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java new file mode 100644 index 0000000000..66f41ee885 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java @@ -0,0 +1,44 @@ +package com.baeldung.junit.tags.example; + +public class Employee { + private int id; + + private String firstName; + + private String lastName; + + private String address; + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(final String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(final String lastName) { + this.lastName = lastName; + } + + public String getAddress() { + return address; + } + + public void setAddress(final String address) { + this.address = address; + } + +} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java new file mode 100644 index 0000000000..21375dbed1 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java @@ -0,0 +1,66 @@ +package com.baeldung.junit.tags.example; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BatchPreparedStatementSetter; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.core.namedparam.SqlParameterSource; +import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +@Repository +public class EmployeeDAO { + + private JdbcTemplate jdbcTemplate; + + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + private SimpleJdbcInsert simpleJdbcInsert; + + @Autowired + public void setDataSource(final DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + + namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); + simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE"); + + } + + public int getCountOfEmployees() { + return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class); + } + + public List getAllEmployees() { + return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper()); + } + + public int addEmplyee(final int id) { + return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA"); + } + + public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) { + final Map parameters = new HashMap(); + parameters.put("ID", emp.getId()); + parameters.put("FIRST_NAME", emp.getFirstName()); + parameters.put("LAST_NAME", emp.getLastName()); + parameters.put("ADDRESS", emp.getAddress()); + + return simpleJdbcInsert.execute(parameters); + } + + // for testing + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java new file mode 100644 index 0000000000..f480aac9a2 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java @@ -0,0 +1,21 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class EmployeeRowMapper implements RowMapper { + + @Override + public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException { + final Employee employee = new Employee(); + + employee.setId(rs.getInt("ID")); + employee.setFirstName(rs.getString("FIRST_NAME")); + employee.setLastName(rs.getString("LAST_NAME")); + employee.setAddress(rs.getString("ADDRESS")); + + return employee; + } +} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java new file mode 100644 index 0000000000..3ddef7e5d4 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; + +@Configuration +@ComponentScan("com.baeldung.junit.tags.example") +public class SpringJdbcConfig { + + @Bean + public DataSource dataSource() { + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build(); + } +} \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql b/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql new file mode 100644 index 0000000000..c86d35cdae --- /dev/null +++ b/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql @@ -0,0 +1,7 @@ +CREATE TABLE EMPLOYEE +( + ID int NOT NULL PRIMARY KEY, + FIRST_NAME varchar(255), + LAST_NAME varchar(255), + ADDRESS varchar(255), +); \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml b/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml new file mode 100644 index 0000000000..5fd2699b41 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql b/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql new file mode 100644 index 0000000000..b9ef8fec25 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql @@ -0,0 +1,7 @@ +INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada'); + +INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA'); + +INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland'); + +INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA'); \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java new file mode 100644 index 0000000000..202ac728a6 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java @@ -0,0 +1,67 @@ +package com.baeldung.categories; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOCategoryIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Category(IntegrationTest.class) + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Category(UnitTest.class) + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } + +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java new file mode 100644 index 0000000000..252e7ffe64 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java @@ -0,0 +1,12 @@ +package com.baeldung.categories; + +import org.junit.experimental.categories.Categories; +import org.junit.experimental.categories.Categories.IncludeCategory; +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Categories.class) +@IncludeCategory(UnitTest.class) +@SuiteClasses(EmployeeDAOCategoryIntegrationTest.class) +public class EmployeeDAOUnitTestSuite { +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java new file mode 100644 index 0000000000..9ced880ffc --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java @@ -0,0 +1,4 @@ +package com.baeldung.categories; + +public interface IntegrationTest { +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java new file mode 100644 index 0000000000..3790fc1d91 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java @@ -0,0 +1,4 @@ +package com.baeldung.categories; + +public interface UnitTest { +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java new file mode 100644 index 0000000000..9b95b3701d --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.junit.tags.example; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Test + public void testQueryMethod() { + Assert.assertEquals(employeeDao.getAllEmployees().size(), 4); + } + + @Test + public void testUpdateMethod() { + Assert.assertEquals(employeeDao.addEmplyee(5), 1); + } + + @Test + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(11); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java new file mode 100644 index 0000000000..6ffb5586cc --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.junit.tags.example; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.jdbc.core.JdbcTemplate; + +public class EmployeeUnitTest { + + @Mock + private JdbcTemplate jdbcTemplate; + + private EmployeeDAO employeeDAO; + + @BeforeEach + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java new file mode 100644 index 0000000000..1b1518337d --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java @@ -0,0 +1,69 @@ +package com.baeldung.tags; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.categories.IntegrationTest; +import com.baeldung.categories.UnitTest; +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOCategoryIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Category(IntegrationTest.class) + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Category(UnitTest.class) + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } + +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java new file mode 100644 index 0000000000..7c16f5eb29 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.tags; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @BeforeEach + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Tag("IntegrationTest") + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assertions.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Tag("UnitTest") + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assertions.assertEquals(1, countOfEmployees); + } + +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java new file mode 100644 index 0000000000..783e5a81a2 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java @@ -0,0 +1,12 @@ +package com.baeldung.tags; + +import org.junit.platform.runner.JUnitPlatform; +import org.junit.platform.suite.api.IncludeTags; +import org.junit.platform.suite.api.SelectPackages; +import org.junit.runner.RunWith; + +@RunWith(JUnitPlatform.class) +@SelectPackages("com.baeldung.tags") +@IncludeTags("UnitTest") +public class EmployeeDAOTestSuite { +} diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 39047fb756..45952efb00 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -32,5 +32,6 @@ test-containers testing testng + junit-5-2