Testing of DAO Layer
Locating and fixing bugs and performance problems during source code development time is arguably the cheapest way to resolve problems. As our project goes past development phase into testing and deployment, the cost of fixing problems grows exponentially. Java testing ensure that the problems in source code are addressed as and when we are developing.
Two most common types of software testing are:
- Unit tests - Tests the smallest unit of functionality, typically a method. They shouldn't call out into non-trivial collaborators, access the network, database, file system, spin up a thread etc. Any kind of dependency that is slow / hard to understand / initialize / manipulate should be mocked using the appropriate technique so we can focus on what the tested unit of code is doing, not what it's dependencies do.
- Integration tests - Build on unit tests by combining the units of code and testing that the resulting combination functions correctly. They can use threads, access the database or network etc. The main advantage of this type of tests is that they will find bugs that unit tests can't - such as wiring bugs and environment bugs. Their main disadvantage is that they touch much more code, are less reliable, failures are harder to diagnose and the tests are harder to maintain.
Based on previous classification of testing strategy types, if we want to write unit tests for our DAO layer, we should mock our database in order to test it in isolation of other parts of our system. That is a bad decision for the following reasons:
- It can be very tedious task that waste too much time.
- Good practice is to test DAO layer as it will be used in production. That means that our tests should use the same configuration as it will be used in production. There are no point to use mocked database or in-memory database for testing and than in production use standalone RDBMS.
There is no sense to isolate DAO code from used data storage and test it separately. In other words, we should write integration tests.
Also, when we are testing code, we want to test our code, not a framework. For example, as we can see in previous sub-chapter, we create our repositories by extending Repository
interface and we do not write any code except the list of methods that we want to expose. Spring Data JPA do all the job for us and it is a well tested project - so there is no need to test it. Basically, this means, that we should test only methods that we are actually write (for example, methods with @Query
annotation), not methods that Spring Data JPA provide to us. The only reason why we might want to test all methods is in case of legacy databases when we want to test if we map correctly tables from database to our entities. In our particular case, as we use in-memory H2 database which one is created every time when we start application, there is no point to test mapping, but for the completeness sake, in following tests we test all methods from our repositories.
Setting up
We are going to use DbUnit for setting up a dataset before each test run. DbUnit is a JUnit extension targeted at database-driven projects that, among other things, puts our database into a known state between test runs. So before we start testing, we have to add the following dependencies:
<!-- DBUnit -->
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>${spring-test-dbunit.version}</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>${dbunit.version}</version>
</dependency>
where versions are defined in <properties>
tag:
<spring-test-dbunit.version>1.2.1</spring-test-dbunit.version>
<dbunit.version>2.5.0</dbunit.version>
Next, inside the src/test/java
folder, we should create a package pms.model.repositories
and that is a package where we will put our tests for repositories. Also, inside the src/test/resources
folder we should create a new folder called datasets
- place where we will put datasets for our tests.
All our tests will use the some same annotation, so let's create AbstractRepositoryIT.java
which one will be extended by all our repository tests in order to remove code duplication:
package pms.model.repositories;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import pms.PmsApplication;
@TestExecutionListeners({DbUnitTestExecutionListener.class})
@SpringApplicationConfiguration(classes = PmsApplication.class)
@DirtiesContext
public abstract class AbstractRepositoryIT extends AbstractTransactionalJUnit4SpringContextTests {
}
Those annotations are:
@TestExecutionListeners
- provide various types of functionality to tests running in the Spring TestContext Framework. They are a way to externalize reusable code that instruments our tests. In our case we useDbUnitTestExecutionListener
which provides support for@DatabaseSetup
,@DatabaseTearDown
and@ExpectedDatabase
annotations.@SpringApplicationConfiguration
- used to determine how to load and configure anApplicationContext
for integration tests.@DirtiesContext
- indicates that theApplicationContext
associated with a test is dirty and should therefore be closed and removed from the context cache.
Repository tests
ManagerRepository integration test
Before we create test for ManagerRepository
we sholud create a dataset with test data.
it-repo-manager.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<MANAGER id="1" name="Manager 1"/>
<MANAGER id="2" name="Manager 2"/>
<MANAGER id="3" name="Manager 3"/>
<PROJECT id="1" budget="10000" deadline="2015-03-01 00:00:00" priority="H" title="Project 1" manager_ID="1" />
<PROJECT id="2" budget="20000" deadline="2015-04-01 00:00:00" priority="L" title="Project 2" manager_ID="1" />
<PROJECT id="3" budget="30000" deadline="2015-05-01 00:00:00" priority="H" title="Project 3" manager_ID="2" />
</dataset>
ManagerRepositoryIT.java
package pms.model.repositories;
import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import com.google.common.base.Optional;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import pms.model.entities.Manager;
import java.util.List;
import static org.junit.Assert.*;
@DatabaseSetup(ManagerRepositoryIT.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = {ManagerRepositoryIT.DATASET})
public class ManagerRepositoryIT extends AbstractRepositoryIT {
protected static final String DATASET = "classpath:datasets/it-repo-manager.xml";
protected static final Long MANAGER_1_ID = (long) 1;
protected static final String MANAGER_1_NAME = "Manager 1";
protected static final Long MANAGER_3_ID = (long) 3;
protected static final long MANAGER_4_ID = (long) 4;
protected static final String MANAGER_4_NAME = "Manager 4";
protected static final String MANAGER_11_NAME = "Manager 11";
@Autowired
private ManagerRepository repository;
@Test
public void testFindAll() {
List<Manager> got = repository.findAll();
assertEquals(3, got.size());
}
@Test
public void testFindOneExisting() {
Optional<Manager> got = repository.findOne(MANAGER_1_ID);
assertTrue(got.isPresent());
Manager manager = got.get();
assertEquals(MANAGER_1_ID, manager.getId());
assertEquals(MANAGER_1_NAME, manager.getName());
}
@Test
public void testFindOneNonExisting() {
Optional<Manager> got = repository.findOne(MANAGER_4_ID);
assertFalse(got.isPresent());
}
@Test
public void testSaveNew() {
Manager managerToBeSaved = new Manager(MANAGER_4_NAME);
Manager got = repository.save(managerToBeSaved);
assertEquals(MANAGER_4_NAME, got.getName());
assertEquals(4, repository.findAll().size());
}
@Test
public void testSaveExisting() {
Manager manager = repository.findOne(MANAGER_1_ID).get();
manager.setName(MANAGER_4_NAME);
repository.save(manager);
Manager mng = repository.findOne(MANAGER_1_ID).get();
assertEquals(MANAGER_1_ID, mng.getId());
assertEquals(MANAGER_4_NAME, mng.getName());
}
@Test(expected = DataIntegrityViolationException.class)
public void testDeleteExistingWithProjects() {
Manager managerToBeDeleted = repository.findOne(MANAGER_1_ID).get();
repository.delete(managerToBeDeleted);
assertEquals(2, repository.findAll().size());
assertFalse(repository.findOne(MANAGER_1_ID).isPresent());
}
@Test
public void testDeleteExistingWithoutProjects() {
Manager managerToBeDeleted = repository.findOne(MANAGER_3_ID).get();
repository.delete(managerToBeDeleted);
assertEquals(2, repository.findAll().size());
assertFalse(repository.findOne(MANAGER_3_ID).isPresent());
}
@Test
public void testDeleteNonExisting() {
Manager managerToBeDeleted = new Manager(MANAGER_4_NAME);
repository.delete(managerToBeDeleted);
assertEquals(3, repository.findAll().size());
}
@Test
public void testFindByNameExisting() {
Manager manager = repository.findByName(MANAGER_1_NAME).get();
assertEquals(MANAGER_1_ID, manager.getId());
assertEquals(MANAGER_1_NAME, manager.getName());
}
@Test
public void testFindByNameNonExisting() {
Optional<Manager> got = repository.findByName(MANAGER_4_NAME);
assertFalse(got.isPresent());
}
@Test
public void testFindByNameLikeExisting() {
Manager managerToBeSaved = new Manager(MANAGER_11_NAME);
repository.save(managerToBeSaved);
assertEquals(2, repository.findByNameLike(MANAGER_1_NAME + "%").size());
}
@Test
public void testFindByNameLikeNonExisting() {
assertEquals(0, repository.findByNameLike(MANAGER_4_NAME + "%").size());
}
}
Test is self-explanatory, we have only to additional annotations:
@DatabaseSetup
- indicates how to put a database into a known state before tests are run.@DatabseTearDown
- indicates how to put a database into a known state after tests are run.
DepartmentRepository integration test
it-repo-department.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<DEPARTMENT id="1" name="Department 1"/>
<DEPARTMENT id="2" name="Department 2"/>
<EMPLOYEE id="1" name="Employee 1" department_ID="1"/>
</dataset>
DepartmentRepositoryIT.java
package pms.model.repositories;
import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import com.google.common.base.Optional;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import pms.model.entities.Department;
import java.util.List;
import static org.junit.Assert.*;
@DatabaseSetup(DepartmentRepositoryIT.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = {DepartmentRepositoryIT.DATASET})
public class DepartmentRepositoryIT extends AbstractRepositoryIT {
protected static final String DATASET = "classpath:datasets/it-repo-department.xml";
protected static final Long DEPARTMENT_1_ID = (long) 1;
protected static final String DEPARTMENT_1_NAME = "Department 1";
protected static final Long DEPARTMENT_2_ID = (long) 2;
protected static final Long DEPARTMENT_3_ID = (long) 3;
protected static final String DEPARTMENT_3_NAME = "Department 3";
@Autowired
private DepartmentRepository repository;
@Test
public void testFindAll() {
List<Department> got = repository.findAll();
assertEquals(2, got.size());
}
@Test
public void testFindOneExisting() {
Optional<Department> got = repository.findOne(DEPARTMENT_1_ID);
assertTrue(got.isPresent());
Department department = got.get();
assertEquals(DEPARTMENT_1_ID, department.getId());
assertEquals(DEPARTMENT_1_NAME, department.getName());
}
@Test
public void testFindOneNonExisting() {
Optional<Department> got = repository.findOne(DEPARTMENT_3_ID);
assertFalse(got.isPresent());
}
@Test
public void testSaveNew() {
Department departmentToBeSaved = new Department(DEPARTMENT_3_NAME);
Department got = repository.save(departmentToBeSaved);
assertEquals(DEPARTMENT_3_NAME, got.getName());
assertEquals(3, repository.findAll().size());
}
@Test
public void testSaveExisting() {
Department department = repository.findOne(DEPARTMENT_1_ID).get();
department.setName(DEPARTMENT_3_NAME);
repository.save(department);
Department dep = repository.findOne(DEPARTMENT_1_ID).get();
assertEquals(DEPARTMENT_1_ID, dep.getId());
assertEquals(DEPARTMENT_3_NAME, dep.getName());
}
@Test(expected = DataIntegrityViolationException.class)
public void testDeleteExistingWithEmployees() {
Department departmentToBeDeleted = repository.findOne(DEPARTMENT_1_ID).get();
repository.delete(departmentToBeDeleted);
assertEquals(1, repository.findAll().size());
assertFalse(repository.findOne(DEPARTMENT_1_ID).isPresent());
}
@Test
public void testDeleteExistingWithoutEmployees() {
Department departmentToBeDeleted = repository.findOne(DEPARTMENT_2_ID).get();
repository.delete(departmentToBeDeleted);
assertEquals(1, repository.findAll().size());
assertFalse(repository.findOne(DEPARTMENT_2_ID).isPresent());
}
@Test
public void testDeleteNonExisting() {
Department departmentToBeDeleted = new Department(DEPARTMENT_3_NAME);
repository.delete(departmentToBeDeleted);
assertEquals(2, repository.findAll().size());
}
@Test
public void testFindByNameExisting() {
Department department = repository.findByName(DEPARTMENT_1_NAME).get();
assertEquals(DEPARTMENT_1_ID, department.getId());
assertEquals(DEPARTMENT_1_NAME, department.getName());
}
@Test
public void testFindByNameNonExisting() {
Optional<Department> got = repository.findByName(DEPARTMENT_3_NAME);
assertFalse(got.isPresent());
}
}
EmployeeRepository integration test
it-repo-employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<DEPARTMENT id="1" name="Department 1"/>
<DEPARTMENT id="2" name="Department 2"/>
<DEPARTMENT id="3" name="Department 3"/>
<EMPLOYEE id="1" name="Employee 1" department_ID="1"/>
<EMPLOYEE id="2" name="Employee 2" department_ID="1"/>
<EMPLOYEE id="3" name="Employee 3" department_ID="1"/>
<EMPLOYEE id="4" name="Employee 4" department_ID="2"/>
<EMPLOYEE id="5" name="Employee 5" department_ID="2"/>
</dataset>
EmployeeRepositoryIT.java
package pms.model.repositories;
import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import com.google.common.base.Optional;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import pms.model.entities.Department;
import pms.model.entities.Employee;
import java.util.List;
import static junit.framework.Assert.*;
@DatabaseSetup(EmployeeRepositoryIT.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = {EmployeeRepositoryIT.DATASET})
public class EmployeeRepositoryIT extends AbstractRepositoryIT {
protected static final String DATASET = "classpath:datasets/it-repo-employee.xml";
protected static final Long EMPLOYEE_1_ID = (long) 1;
protected static final String EMPLOYEE_1_NAME = "Employee 1";
protected static final Long EMPLOYEE_1_DEPARTMENT_ID = (long) 1;
protected static final String EMPLOYEE_1_DEPARTMENT_NAME = "Department 1";
protected static final Long EMPLOYEE_6_ID = (long) 6;
protected static final String EMPLOYEE_6_NAME = "Employee 6";
protected static final String EMPLOYEE_11_NAME = "Employee 11";
protected static final String DEPARTMENT_3_NAME = "Department 3";
protected static final String DEPARTMENT_4_NAME = "Department 4";
@Autowired
private EmployeeRepository repository;
@Test
public void testFindAll() {
List<Employee> got = repository.findAll();
assertEquals(5, got.size());
}
@Test
public void testFindOneExisting() {
Optional<Employee> got = repository.findOne(EMPLOYEE_1_ID);
assertTrue(got.isPresent());
Employee employee = got.get();
assertEquals(EMPLOYEE_1_ID, employee.getId());
assertEquals(EMPLOYEE_1_NAME, employee.getName());
assertEquals(EMPLOYEE_1_DEPARTMENT_ID, employee.getDepartment().getId());
}
@Test
public void testFindOneNonExisting() {
Optional<Employee> got = repository.findOne(EMPLOYEE_6_ID);
assertFalse(got.isPresent());
}
@Test
public void testSaveNew() {
Employee employeeToBeSaved = new Employee(EMPLOYEE_6_NAME);
Employee got = repository.save(employeeToBeSaved);
assertEquals(EMPLOYEE_6_NAME, got.getName());
assertEquals(6, repository.findAll().size());
}
@Test
public void testSaveExisting() {
Employee employee = repository.findOne(EMPLOYEE_1_ID).get();
employee.setName(EMPLOYEE_6_NAME);
repository.save(employee);
Employee emp = repository.findOne(EMPLOYEE_1_ID).get();
assertEquals(EMPLOYEE_1_ID, emp.getId());
assertEquals(EMPLOYEE_6_NAME, emp.getName());
assertEquals(EMPLOYEE_1_DEPARTMENT_ID, emp.getDepartment().getId());
}
@Test
public void testDeleteExisting() {
Employee employeeToBeDeleted = repository.findOne(EMPLOYEE_1_ID).get();
repository.delete(employeeToBeDeleted);
assertEquals(4, repository.findAll().size());
assertFalse(repository.findOne(EMPLOYEE_1_ID).isPresent());
}
@Test
public void testDeleteNonExisting() {
Employee employeeToBeDeleted = new Employee(EMPLOYEE_6_NAME);
repository.delete(employeeToBeDeleted);
assertEquals(5, repository.findAll().size());
}
@Test
public void testFindByNameExisting() {
Employee employee = repository.findByName(EMPLOYEE_1_NAME).get();
assertEquals(EMPLOYEE_1_ID, employee.getId());
assertEquals(EMPLOYEE_1_NAME, employee.getName());
}
@Test
public void testFindByNameNonExisting() {
Optional<Employee> got = repository.findByName(EMPLOYEE_6_NAME);
assertFalse(got.isPresent());
}
@Test
public void testFindByNameLikeExisting() {
Employee employeeToBeSaved = new Employee(EMPLOYEE_11_NAME);
repository.save(employeeToBeSaved);
assertEquals(2, repository.findByNameLike(EMPLOYEE_1_NAME + "%").size());
}
@Test
public void testFindByNameLikeNonExisting() {
assertEquals(0, repository.findByNameLike(EMPLOYEE_6_NAME + "%").size());
}
@Test
public void testFindByDepartmentExistingWithEmployees() {
Department department = new Department(EMPLOYEE_1_DEPARTMENT_NAME);
department.setId((long) 1);
assertEquals(3, repository.findByDepartment(department).size());
}
@Test
public void testFindByDepartmentExistingWithoutEmployees() {
Department department = new Department(DEPARTMENT_3_NAME);
department.setId((long) 3);
assertEquals(0, repository.findByDepartment(department).size());
}
@Test
public void testFindByDepartmentNonExisting() {
Department department = new Department(DEPARTMENT_4_NAME);
department.setId((long) 4);
assertEquals(0, repository.findByDepartment(department).size());
}
}
ProjectRepository integration test
it-repo-project.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<MANAGER id="1" name="Manager 1"/>
<MANAGER id="2" name="Manager 2"/>
<MANAGER id="3" name="Manager 3"/>
<PROJECT id="1" budget="10000" deadline="2015-03-01 00:00:00" priority="H" title="Project 1" manager_ID="1" />
<PROJECT id="2" budget="20000" deadline="2015-04-01 00:00:00" priority="L" title="Project 2" manager_ID="1" />
<PROJECT id="3" budget="30000" deadline="2015-05-01 00:00:00" priority="H" title="Project 3" manager_ID="2" />
<DEPARTMENT id="1" name="Department 1"/>
<DEPARTMENT id="2" name="Department 2"/>
<EMPLOYEE id="1" name="Employee 1" department_ID="1"/>
<EMPLOYEE id="2" name="Employee 2" department_ID="1"/>
<EMPLOYEE id="3" name="Employee 3" department_ID="1"/>
<EMPLOYEE id="4" name="Employee 4" department_ID="2"/>
<EMPLOYEE id="5" name="Employee 5" department_ID="2"/>
<PROJECT_EMPLOYEE id_project="1" id_employee="1" hourly_rate="5.0"/>
<PROJECT_EMPLOYEE id_project="1" id_employee="2" hourly_rate="4.5"/>
<PROJECT_EMPLOYEE id_project="1" id_employee="4" hourly_rate="5.0"/>
<PROJECT_EMPLOYEE id_project="2" id_employee="1" hourly_rate="5.5"/>
<PROJECT_EMPLOYEE id_project="2" id_employee="3" hourly_rate="6.0"/>
<PROJECT_EMPLOYEE id_project="2" id_employee="5" hourly_rate="7.0"/>
</dataset>
ProjectRepositoryIT.java
package pms.model.repositories;
import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import com.google.common.base.Optional;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import pms.model.entities.Manager;
import pms.model.entities.Project;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static junit.framework.Assert.*;
@DatabaseSetup(ProjectRepositoryIT.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = {ProjectRepositoryIT.DATASET})
public class ProjectRepositoryIT extends AbstractRepositoryIT {
protected static final String DATASET = "classpath:datasets/it-repo-project.xml";
protected static final int PROJECT_NUMBER = 3;
protected static final Long PROJECT_1_ID = (long) 1;
protected static final String PROJECT_1_TITLE = "Project 1";
protected static final Long PROJECT_4_ID = (long) 4;
protected static final String PROJECT_4_TITLE = "Project 4";
protected static final BigDecimal PROJECT_4_BUDGET = new BigDecimal(5000);
protected static final Date PROJECT_4_DEADLINE = new Date();
protected static final String PROJECT_11_TITLE = "Project 11";
protected static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
protected static final Long MANAGER_1_ID = (long) 1;
protected static final String MANAGER_1_NAME = "Manager 1";
protected static final Long MANAGER_3_ID = (long) 3;
protected static final String MANAGER_3_NAME = "Manager 3";
@Autowired
private ProjectRepository repository;
@Test
public void testFindAll() {
List<Project> got = repository.findAll();
assertEquals(PROJECT_NUMBER, got.size());
}
@Test
public void testFindOneExisting() {
Optional<Project> got = repository.findOne(PROJECT_1_ID);
assertTrue(got.isPresent());
Project project = got.get();
assertEquals(PROJECT_1_ID, project.getId());
assertEquals(PROJECT_1_TITLE, project.getTitle());
}
@Test
public void testFindOneNonExisting() {
Optional<Project> got = repository.findOne(PROJECT_4_ID);
assertFalse(got.isPresent());
}
@Test
public void testSaveNew() {
Project projectToBeSaved = new Project(PROJECT_4_TITLE, PROJECT_4_BUDGET, PROJECT_4_DEADLINE);
Project got = repository.save(projectToBeSaved);
assertEquals(PROJECT_4_TITLE, got.getTitle());
assertEquals(PROJECT_NUMBER + 1, repository.findAll().size());
}
@Test
public void testSaveExisting() {
Project project = repository.findOne(PROJECT_1_ID).get();
project.setTitle(PROJECT_4_TITLE);
repository.save(project);
Project prj = repository.findOne(PROJECT_1_ID).get();
assertEquals(PROJECT_1_ID, prj.getId());
assertEquals(PROJECT_4_TITLE, prj.getTitle());
}
@Test
public void testDeleteExisting() {
Project projectToBeDeleted = repository.findOne(PROJECT_1_ID).get();
repository.delete(projectToBeDeleted);
assertEquals(PROJECT_NUMBER - 1, repository.findAll().size());
assertFalse(repository.findOne(PROJECT_1_ID).isPresent());
}
@Test
public void testDeleteNonExisting() {
Project projectToBeDeleted = new Project(PROJECT_4_TITLE, PROJECT_4_BUDGET, PROJECT_4_DEADLINE);
repository.delete(projectToBeDeleted);
assertEquals(PROJECT_NUMBER, repository.findAll().size());
}
@Test
public void testFindByTitleExisting() {
Project project = repository.findByTitle(PROJECT_1_TITLE).get();
assertEquals(PROJECT_1_ID, project.getId());
assertEquals(PROJECT_1_TITLE, project.getTitle());
}
@Test
public void testFindByTitleNonExisting() {
Optional<Project> got = repository.findByTitle(PROJECT_4_TITLE);
assertFalse(got.isPresent());
}
@Test
public void testFindByTitleLikeExisting() {
Project projectToBeSaved = new Project(PROJECT_11_TITLE, PROJECT_4_BUDGET, PROJECT_4_DEADLINE);
repository.save(projectToBeSaved);
assertEquals(2, repository.findByTitleLike(PROJECT_1_TITLE + "%").size());
}
@Test
public void testFindByTitleLikeNonExisting() {
assertEquals(0, repository.findByTitleLike(PROJECT_4_TITLE + "%").size());
}
@Test
public void testFindByPriorityExisting() {
assertEquals(2, repository.findByPriority("H").size());
}
@Test
public void testFindByPriorityNonExisting() {
assertEquals(0, repository.findByPriority("Something").size());
}
@Test
public void testFindByDeadlineBeforeExisting() throws ParseException {
Date before = formatter.parse("2015-05-01");
assertEquals(2, repository.findByDeadlineBefore(before).size());
}
@Test
public void testFindByDeadlineBeforeNonExisting() throws ParseException {
Date before = formatter.parse("2015-01-01");
assertEquals(0, repository.findByDeadlineBefore(before).size());
}
@Test
public void testFindByDeadlineAfterExisting() throws ParseException {
Date after = formatter.parse("2015-03-01");
assertEquals(2, repository.findByDeadlineAfter(after).size());
}
@Test
public void testFindByDeadlineAfterNonExisting() throws ParseException {
Date after = formatter.parse("2015-05-01");
assertEquals(0, repository.findByDeadlineAfter(after).size());
}
@Test
public void testFindByPriorityAndDeadlineBeforeExisting() throws ParseException {
Date before = formatter.parse("2015-05-01");
assertEquals(1, repository.findByPriorityAndDeadlineBefore("L", before).size());
}
@Test
public void testFindByPriorityAndDeadlineBeforeNonExisting() throws ParseException {
Date before = formatter.parse("2015-05-01");
assertEquals(0, repository.findByPriorityAndDeadlineBefore("Something", before).size());
}
@Test
public void testFindByManagerExisting() {
Manager manager = new Manager(MANAGER_1_NAME);
manager.setId(MANAGER_1_ID);
assertEquals(2, repository.findByManager(manager).size());
}
@Test
public void testFindByManagerNonExisting() {
Manager manager = new Manager(MANAGER_3_NAME);
manager.setId(MANAGER_3_ID);
assertEquals(0, repository.findByManager(manager).size());
}
@Test
public void testFindByPriorityAndManagerExisting() {
Manager manager = new Manager(MANAGER_1_NAME);
manager.setId(MANAGER_1_ID);
assertEquals(1, repository.findByPriorityAndManager("H", manager).size());
}
@Test
public void testFindByPriorityAndManagerNonExisting() {
Manager manager = new Manager(MANAGER_3_NAME);
manager.setId(MANAGER_3_ID);
assertEquals(0, repository.findByPriorityAndManager("H", manager).size());
}
@Test
public void testFindByPriorityManagerAndDeadlineBeforeExisting() throws ParseException {
Date before = formatter.parse("2015-05-01");
Manager manager = new Manager(MANAGER_1_NAME);
manager.setId(MANAGER_1_ID);
assertEquals(1, repository.findByPriorityAndManagerAndDeadlineBefore("L", manager, before).size());
}
@Test
public void testFindByPriorityManagerAndDeadlineBeforeNonExisting() throws ParseException {
Date before = formatter.parse("2015-01-01");
Manager manager = new Manager(MANAGER_1_NAME);
manager.setId(MANAGER_1_ID);
assertEquals(0, repository.findByPriorityAndManagerAndDeadlineBefore("L", manager, before).size());
}
}
Test Suite
If we want to execute all our repository integration tests at once, we can create a test suite:
RepositoryITSuite.java
package pms.model.repositories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ManagerRepositoryIT.class,
DepartmentRepositoryIT.class,
EmployeeRepositoryIT.class,
ProjectRepositoryIT.class
})
public class RepositoryITSuite {
}