separate test datasource (#1575)
* separate test datasource * remove hsqldb * fix var name * move config class
This commit is contained in:
parent
196ea6b5f7
commit
a3926e4204
|
@ -67,6 +67,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -219,6 +220,7 @@
|
||||||
<bootstrap.version>3.3.7-1</bootstrap.version>
|
<bootstrap.version>3.3.7-1</bootstrap.version>
|
||||||
<subethasmtp.version>3.1.7</subethasmtp.version>
|
<subethasmtp.version>3.1.7</subethasmtp.version>
|
||||||
<tomee-servlet-api.version>8.5.11</tomee-servlet-api.version>
|
<tomee-servlet-api.version>8.5.11</tomee-servlet-api.version>
|
||||||
|
<h2.version>1.4.194</h2.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableJpaRepositories(basePackages = { "org.baeldung.repository", "org.baeldung.boot.repository" })
|
||||||
|
@PropertySource("classpath:persistence-generic-entity.properties")
|
||||||
|
@EnableTransactionManagement
|
||||||
|
public class H2JpaConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
|
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
||||||
|
dataSource.setUrl(env.getProperty("jdbc.url"));
|
||||||
|
dataSource.setUsername(env.getProperty("jdbc.user"));
|
||||||
|
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||||
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
em.setDataSource(dataSource());
|
||||||
|
em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.model" });
|
||||||
|
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||||
|
em.setJpaProperties(additionalProperties());
|
||||||
|
return em;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
|
||||||
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
|
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Properties additionalProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
|
||||||
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
|
||||||
|
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package org.baeldung;
|
||||||
|
|
||||||
|
import org.baeldung.config.H2JpaConfig;
|
||||||
|
import org.baeldung.domain.GenericEntity;
|
||||||
|
import org.baeldung.repository.GenericEntityRepository;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringBootTest(classes = { Application.class, H2JpaConfig.class })
|
||||||
|
public class SpringBootH2IntegrationTest {
|
||||||
|
@Autowired
|
||||||
|
private GenericEntityRepository genericEntityRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() {
|
||||||
|
GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test"));
|
||||||
|
GenericEntity foundEntity = genericEntityRepository.findOne(genericEntity.getId());
|
||||||
|
assertNotNull(foundEntity);
|
||||||
|
assertEquals(genericEntity.getValue(), foundEntity.getValue());
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,8 +20,8 @@ public class SpringBootJPAIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() {
|
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() {
|
||||||
GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test"));
|
GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test"));
|
||||||
GenericEntity foundedEntity = genericEntityRepository.findOne(genericEntity.getId());
|
GenericEntity foundEntity = genericEntityRepository.findOne(genericEntity.getId());
|
||||||
assertNotNull(foundedEntity);
|
assertNotNull(foundEntity);
|
||||||
assertEquals(genericEntity.getValue(), foundedEntity.getValue());
|
assertEquals(genericEntity.getValue(), foundEntity.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package org.baeldung;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.baeldung.config.H2TestProfileJPAConfig;
|
||||||
|
import org.baeldung.domain.GenericEntity;
|
||||||
|
import org.baeldung.repository.GenericEntityRepository;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringBootTest(classes = { Application.class, H2TestProfileJPAConfig.class })
|
||||||
|
@ActiveProfiles("test")
|
||||||
|
public class SpringBootProfileIntegrationTest {
|
||||||
|
@Autowired
|
||||||
|
private GenericEntityRepository genericEntityRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() {
|
||||||
|
GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test"));
|
||||||
|
GenericEntity foundEntity = genericEntityRepository.findOne(genericEntity.getId());
|
||||||
|
assertNotNull(foundEntity);
|
||||||
|
assertEquals(genericEntity.getValue(), foundEntity.getValue());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableJpaRepositories(basePackages = { "org.baeldung.repository", "org.baeldung.boot.repository" })
|
||||||
|
@EnableTransactionManagement
|
||||||
|
public class H2TestProfileJPAConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Profile("test")
|
||||||
|
public DataSource dataSource() {
|
||||||
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
|
dataSource.setDriverClassName("org.h2.Driver");
|
||||||
|
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
|
||||||
|
dataSource.setUsername("sa");
|
||||||
|
dataSource.setPassword("sa");
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||||
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
em.setDataSource(dataSource());
|
||||||
|
em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.model" });
|
||||||
|
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||||
|
em.setJpaProperties(additionalProperties());
|
||||||
|
return em;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
|
||||||
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
|
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Properties additionalProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
|
||||||
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
|
||||||
|
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,4 +2,18 @@ spring.mail.host=localhost
|
||||||
spring.mail.port=8025
|
spring.mail.port=8025
|
||||||
spring.mail.properties.mail.smtp.auth=false
|
spring.mail.properties.mail.smtp.auth=false
|
||||||
|
|
||||||
security.basic.enabled=false
|
security.basic.enabled=false
|
||||||
|
|
||||||
|
# spring.datasource.x
|
||||||
|
spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=sa
|
||||||
|
|
||||||
|
# hibernate.X
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
hibernate.cache.use_second_level_cache=true
|
||||||
|
hibernate.cache.use_query_cache=true
|
||||||
|
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
|
@ -0,0 +1,8 @@
|
||||||
|
jdbc.driverClassName=org.h2.Driver
|
||||||
|
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||||
|
jdbc.username=sa
|
||||||
|
jdbc.password=sa
|
||||||
|
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
Loading…
Reference in New Issue