[JAVA-2306] Moved articles from spring-persistence-simple-2

* https://www.baeldung.com/spring-jdbctemplate-testing went to spring-jdbc

* https://www.baeldung.com/spring-jdbctemplate-in-list went to spring-jdbc

* https://www.baeldung.com/spring-mock-jndi-datasource went to spring-persistence-simple

* Deleted spring-persistence-simple-2 module as all articles have been moved
This commit is contained in:
fdpro
2020-08-30 15:59:35 +02:00
parent 4e4ac650fa
commit eb4c306451
30 changed files with 224 additions and 89 deletions
@@ -1,9 +1,11 @@
package com.baeldung.spring.jdbc;
package com.baeldung.spring.jdbc.template.guide;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.spring.jdbc.config.SpringJdbcConfig;
import com.baeldung.spring.jdbc.template.guide.Employee;
import com.baeldung.spring.jdbc.template.guide.EmployeeDAO;
import com.baeldung.spring.jdbc.template.guide.config.SpringJdbcConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -0,0 +1,80 @@
package com.baeldung.spring.jdbc.template.inclause;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class EmployeeDAOUnitTest {
@Mock
JdbcTemplate jdbcTemplate;
DataSource dataSource;
@Before
public void setup() {
dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.generateUniqueName(true)
.addScript("classpath:com/baeldung/spring/jdbc/template/inclause/schema.sql")
.addScript("classpath:com/baeldung/spring/jdbc/template/inclause/test-data.sql")
.build();
}
@Test
public void givenSmallIdList_whenGetEmployeesFromIdList_thenReturnCorrectEmployees() {
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(3);
ids.add(4);
EmployeeDAO employeeDAO = new EmployeeDAO();
employeeDAO.setDataSource(dataSource);
List<Employee> employees = employeeDAO.getEmployeesFromIdList(ids);
assertEquals(3, employees.size());
assertEquals(1, employees.get(0).getId());
assertEquals(3, employees.get(1).getId());
assertEquals(4, employees.get(2).getId());
employees = employeeDAO.getEmployeesFromIdListNamed(ids);
assertEquals(3, employees.size());
assertEquals(1, employees.get(0).getId());
assertEquals(3, employees.get(1).getId());
assertEquals(4, employees.get(2).getId());
}
@Test
public void givenLargeIdList_whenGetEmployeesFromIdList_thenReturnCorrectEmployees() {
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(3);
ids.add(4);
EmployeeDAO employeeDAO = new EmployeeDAO();
employeeDAO.setDataSource(dataSource);
List<Employee> employees = employeeDAO.getEmployeesFromLargeIdList(ids);
assertEquals(3, employees.size());
assertEquals(1, employees.get(0).getId());
assertEquals(3, employees.get(1).getId());
assertEquals(4, employees.get(2).getId());
ids.clear();
ids.add(2);
employees = employeeDAO.getEmployeesFromLargeIdList(ids);
assertEquals(1, employees.size());
}
}
@@ -0,0 +1,56 @@
package com.baeldung.spring.jdbc.template.testing;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.util.ReflectionTestUtils;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class EmployeeDAOUnitTest {
@Mock
JdbcTemplate jdbcTemplate;
DataSource dataSource;
@Before
public void setup() {
dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.generateUniqueName(true)
.addScript("classpath:com/baeldung/spring/jdbc/template/testing/schema.sql")
.addScript("classpath:com/baeldung/spring/jdbc/template/testing/test-data.sql")
.build();
}
@Test
public void whenMockJdbcTemplate_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
ReflectionTestUtils.setField(employeeDAO, "jdbcTemplate", jdbcTemplate);
Mockito.when(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class))
.thenReturn(4);
assertEquals(4, employeeDAO.getCountOfEmployees());
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Mockito.eq(Integer.class)))
.thenReturn(3);
assertEquals(3, employeeDAO.getCountOfEmployees());
}
@Test
public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
employeeDAO.setDataSource(dataSource);
assertEquals(4, employeeDAO.getCountOfEmployees());
}
}