[BAEL-7645] - Fixed integration test of spring-jpa module through H2 inmemory DB

This commit is contained in:
amit2103 2018-07-16 23:25:55 +05:30
parent 362808140a
commit 7063b13578
20 changed files with 45 additions and 18 deletions

View File

@ -24,7 +24,7 @@ import com.google.common.base.Preconditions;
@Configuration @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" }) @PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "org.baeldung.persistence" }) @ComponentScan({ "org.baeldung.persistence" })
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") @EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
public class PersistenceJPAConfig { public class PersistenceJPAConfig {

View File

@ -11,7 +11,7 @@ import javax.persistence.Table;
public class Possession { public class Possession {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private long id;
private String name; private String name;

View File

@ -15,7 +15,7 @@ import javax.persistence.Table;
public class User { public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private int id;
private String name; private String name;

View File

@ -2,7 +2,7 @@
jdbc.driverClassName=org.h2.Driver jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
jdbc.user=sa jdbc.user=sa
# jdbc.pass= jdbc.pass=
# hibernate.X # hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.dialect=org.hibernate.dialect.H2Dialect

View File

@ -1,12 +1,12 @@
# jdbc.X # jdbc.X
jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.driverClassName=org.h2.Driver
user.jdbc.url=jdbc:mysql://localhost:3306/spring_jpa_user?createDatabaseIfNotExist=true user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS SPRING_JPA_USER
product.jdbc.url=jdbc:mysql://localhost:3306/spring_jpa_product?createDatabaseIfNotExist=true product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS SPRING_JPA_PRODUCT
jdbc.user=tutorialuser jdbc.user=sa
jdbc.pass=tutorialmy5ql jdbc.pass=
# hibernate.X # hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=false hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=false hibernate.cache.use_second_level_cache=false

View File

@ -2,7 +2,7 @@
jdbc.driverClassName=org.h2.Driver jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
jdbc.user=sa jdbc.user=sa
# jdbc.pass= jdbc.pass=
# hibernate.X # hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.dialect=org.hibernate.dialect.H2Dialect

View File

@ -7,7 +7,7 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
> >
<context:property-placeholder location="classpath:persistence-mysql.properties"/> <context:property-placeholder location="classpath:persistence-h2.properties"/>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/> <property name="dataSource" ref="dataSource"/>

View File

@ -9,11 +9,13 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = DataSourceRoutingTestConfiguration.class) @ContextConfiguration(classes = DataSourceRoutingTestConfiguration.class)
@DirtiesContext
public class DataSourceRoutingIntegrationTest { public class DataSourceRoutingIntegrationTest {
@Autowired @Autowired

View File

@ -10,6 +10,6 @@ public class PersistenceJPAConfigDeletion extends PersistenceJPAConfigL2Cache {
@Override @Override
protected String[] getPackagesToScan() { protected String[] getPackagesToScan() {
return new String[] { "org.baeldung.persistence.deletion.model" }; return new String[] { "org.baeldung.persistence.deletion.model", "org.baeldung.persistence.model" };
} }
} }

View File

@ -11,6 +11,7 @@ import org.baeldung.inmemory.persistence.model.SkillTag;
import org.baeldung.inmemory.persistence.model.Student; import org.baeldung.inmemory.persistence.model.Student;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -26,6 +27,7 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class)
@Transactional @Transactional
@DirtiesContext
public class AdvancedTaggingIntegrationTest { public class AdvancedTaggingIntegrationTest {
@Resource @Resource
private StudentRepository studentRepository; private StudentRepository studentRepository;

View File

@ -12,11 +12,13 @@ import org.baeldung.inmemory.persistence.model.Student;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { StudentJPAH2Config.class }) @ContextConfiguration(classes = { StudentJPAH2Config.class })
@DirtiesContext
public class ExtendedStudentRepositoryIntegrationTest { public class ExtendedStudentRepositoryIntegrationTest {
@Resource @Resource
private ExtendedStudentRepository extendedStudentRepository; private ExtendedStudentRepository extendedStudentRepository;

View File

@ -5,6 +5,7 @@ import org.baeldung.inmemory.persistence.dao.StudentRepository;
import org.baeldung.inmemory.persistence.model.Student; import org.baeldung.inmemory.persistence.model.Student;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -20,6 +21,7 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class)
@Transactional @Transactional
@DirtiesContext
public class InMemoryDBIntegrationTest { public class InMemoryDBIntegrationTest {
@Resource @Resource

View File

@ -11,6 +11,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.JpaSort; import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.data.mapping.PropertyReferenceException; import org.springframework.data.mapping.PropertyReferenceException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -25,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = PersistenceJPAConfigL2Cache.class) @ContextConfiguration(classes = PersistenceJPAConfigL2Cache.class)
@DirtiesContext
public class UserRepositoryIntegrationTest { public class UserRepositoryIntegrationTest {
private final String USER_NAME_ADAM = "Adam"; private final String USER_NAME_ADAM = "Adam";

View File

@ -8,6 +8,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -23,6 +24,7 @@ import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPAConfigDeletion.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceJPAConfigDeletion.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext
public class DeletionIntegrationTest { public class DeletionIntegrationTest {
@PersistenceContext @PersistenceContext

View File

@ -16,17 +16,20 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import org.baeldung.config.PersistenceJPAConfig; import org.baeldung.config.PersistenceJPAConfig;
import org.baeldung.config.PersistenceJPAConfigL2Cache;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext
public class FooPaginationPersistenceIntegrationTest { public class FooPaginationPersistenceIntegrationTest {
@PersistenceContext @PersistenceContext

View File

@ -3,6 +3,7 @@ package org.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.config.PersistenceJPAConfig; import org.baeldung.config.PersistenceJPAConfig;
import org.baeldung.config.PersistenceJPAConfigL2Cache;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -11,12 +12,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext
public class FooServicePersistenceIntegrationTest { public class FooServicePersistenceIntegrationTest {
@Autowired @Autowired

View File

@ -10,17 +10,19 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import org.baeldung.config.PersistenceJPAConfig; import org.baeldung.config.PersistenceJPAConfigL2Cache;
import org.baeldung.persistence.model.Bar; import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class FooServiceSortingIntegrationTest { public class FooServiceSortingIntegrationTest {

View File

@ -10,16 +10,19 @@ import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import org.baeldung.config.PersistenceJPAConfig; import org.baeldung.config.PersistenceJPAConfig;
import org.baeldung.config.PersistenceJPAConfigL2Cache;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext
public class FooServiceSortingWitNullsManualIntegrationTest { public class FooServiceSortingWitNullsManualIntegrationTest {
@PersistenceContext @PersistenceContext

View File

@ -18,6 +18,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@ -26,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { UserConfig.class, ProductConfig.class }) @ContextConfiguration(classes = { UserConfig.class, ProductConfig.class })
@EnableTransactionManagement @EnableTransactionManagement
@DirtiesContext
public class JpaMultipleDBIntegrationTest { public class JpaMultipleDBIntegrationTest {
@Autowired @Autowired

View File

@ -8,6 +8,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -24,6 +25,7 @@ import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext
public class SecondLevelCacheIntegrationTest { public class SecondLevelCacheIntegrationTest {
@PersistenceContext @PersistenceContext