Using H2 for data base integrationt tests
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 25 Dec 2016 18:26:06 +0000 (19:26 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 25 Dec 2016 18:48:59 +0000 (19:48 +0100)
SpringJava/Gradle/spring-jpa-persistence/src/integTest/java/de/spring/example/persistence/repository/AdRespositoryShould.java [new file with mode: 0644]
SpringJava/Gradle/spring-jpa-persistence/src/integTest/resources/spring-configuration-test/datasource-test-configuration.xml [new file with mode: 0644]
SpringJava/Gradle/spring-jpa-persistence/src/main/resources/spring-configuration/liquibase/ddlChangelog.xml
SpringJava/Gradle/spring-jpa-persistence/src/test/resources/datasource-test-configuration.xml [deleted file]

diff --git a/SpringJava/Gradle/spring-jpa-persistence/src/integTest/java/de/spring/example/persistence/repository/AdRespositoryShould.java b/SpringJava/Gradle/spring-jpa-persistence/src/integTest/java/de/spring/example/persistence/repository/AdRespositoryShould.java
new file mode 100644 (file)
index 0000000..2485486
--- /dev/null
@@ -0,0 +1,67 @@
+package de.spring.example.persistence.repository;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.time.OffsetDateTime;
+
+import javax.inject.Inject;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.transaction.annotation.Transactional;
+
+import de.spring.example.persistence.domain.Ad;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration( {"classpath*:spring-configuration/*.xml",
+       "classpath*:spring-configuration-test/*.xml"} )
+@Transactional
+public class AdRespositoryShould {
+       // Ad
+       public static final Long COMPANY_ID = 2L;
+       public static final Long COMPANY_CATEG_ID = 3L;
+       public static final String AD_MOBILE_IMAGE = "slippers.jpg";
+       public static final OffsetDateTime CREATED_AT = OffsetDateTime.now().minusDays(1);
+       public static final OffsetDateTime UPDATED_AT = OffsetDateTime.now();
+
+       @Inject
+       AdRepository adRepository;
+       
+       @Test public void
+       find_one_Ad_by_id() {
+               Ad ad = createAd();
+               Ad createdAd = adRepository.save(ad);
+               
+               Ad storedAd = adRepository.findOne(createdAd.getId());
+               
+               assertThat(createdAd, is(storedAd));
+       }
+       
+       @Test public void
+       find_one_Ad_by_id_using_native_query() {
+               Ad ad = createAd();
+               Ad createdAd = adRepository.save(ad);
+               
+               Ad storedAd = adRepository.findByIdNativeQuery(createdAd.getId());
+                               
+               assertThat(createdAd, is(storedAd));
+       }
+       
+       @Test public void
+       find_one_Ad_by_id_using_named_query() {
+               Ad ad = createAd();
+               Ad createdAd = adRepository.save(ad);
+               
+               Ad storedAd = adRepository.findByIdQuery(createdAd.getId());
+                               
+               assertThat(createdAd, is(storedAd));
+       }
+       
+       private static final Ad createAd() {    
+               return new Ad(null, null, COMPANY_ID, COMPANY_CATEG_ID, AD_MOBILE_IMAGE,
+                               CREATED_AT, UPDATED_AT);
+       }
+}
diff --git a/SpringJava/Gradle/spring-jpa-persistence/src/integTest/resources/spring-configuration-test/datasource-test-configuration.xml b/SpringJava/Gradle/spring-jpa-persistence/src/integTest/resources/spring-configuration-test/datasource-test-configuration.xml
new file mode 100644 (file)
index 0000000..7ec6b17
--- /dev/null
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+       xmlns:util="http://www.springframework.org/schema/util"\r
+       xmlns:tx="http://www.springframework.org/schema/tx"\r
+       xmlns:jee="http://www.springframework.org/schema/jee"\r
+       xsi:schemaLocation="http://www.springframework.org/schema/beans\r
+        http://www.springframework.org/schema/beans/spring-beans.xsd\r
+        http://www.springframework.org/schema/util\r
+        http://www.springframework.org/schema/util/spring-util.xsd\r
+        http://www.springframework.org/schema/tx \r
+        http://www.springframework.org/schema/tx/spring-tx.xsd\r
+        http://www.springframework.org/schema/jee\r
+        http://www.springframework.org/schema/jee/spring-jee.xsd">\r
+\r
+       \r
+       <!-- \r
+               In this way we can not use all the available options for H2.\r
+               \r
+               Because of that I will be using the dataSource bean where I can choose as many options\r
+               as I want for H2.\r
+    <jdbc:embedded-database id="dataSource" type="H2" />\r
+       -->\r
+       \r
+       <!-- \r
+               1. There are two beans with the same id "dataSource"\r
+                  One declared in datasource-configuration.xml\r
+                  Another one declared in datasource-test-configuration.xml\r
+               2. Both beans are declared in different XML files.\r
+               3. Because there are in different XML files Spring does not complain about having duplicate beans.\r
+               4. Because files in src/test will be loaded in class path after files in src/main this bean will\r
+                  override the one declared in datasource-configuration.xml when running JUnit Tests :)\r
+        -->\r
+       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">\r
+               <property name="driverClassName" value="org.h2.Driver" />\r
+               <property name="url" value="jdbc:h2:mem:mybatis_example;INIT=create schema if not exists mybatis_example\;SET SCHEMA mybatis_example;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" />\r
+               <property name="username" value="mybatis" />\r
+               <property name="password" value="" />\r
+       </bean>\r
+       \r
+</beans>\r
index e972618..2fd98a8 100644 (file)
         </createTable>
     </changeSet>
     <changeSet author="gustavo (generated)" id="1469119656864-7" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
-        <addPrimaryKey columnNames="ID, REVISION" constraintName="PRIMARY" tableName="AD_AUDITED"/>
+        <addPrimaryKey columnNames="ID, REVISION" constraintName="PK_AD_AUDITED" tableName="AD_AUDITED"/>
     </changeSet>
     <changeSet author="gustavo (generated)" id="1469119656864-8" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
-        <addPrimaryKey columnNames="ID, REVISION" constraintName="PRIMARY" tableName="AD_DESCRIPTION_AUDITED"/>
+        <addPrimaryKey columnNames="ID, REVISION" constraintName="PK_AD_DESCRIPTION_AUDITED" tableName="AD_DESCRIPTION_AUDITED"/>
     </changeSet>
     <changeSet author="gustavo (generated)" id="1469119656864-10" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
         <createIndex indexName="IND_01_AD_DESCRIPTION" tableName="AD_DESCRIPTION">
diff --git a/SpringJava/Gradle/spring-jpa-persistence/src/test/resources/datasource-test-configuration.xml b/SpringJava/Gradle/spring-jpa-persistence/src/test/resources/datasource-test-configuration.xml
deleted file mode 100644 (file)
index 68cd61f..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<beans xmlns="http://www.springframework.org/schema/beans"\r
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
-       xmlns:util="http://www.springframework.org/schema/util"\r
-       xmlns:tx="http://www.springframework.org/schema/tx"\r
-       xmlns:jee="http://www.springframework.org/schema/jee"\r
-       xsi:schemaLocation="http://www.springframework.org/schema/beans\r
-        http://www.springframework.org/schema/beans/spring-beans.xsd\r
-        http://www.springframework.org/schema/util\r
-        http://www.springframework.org/schema/util/spring-util.xsd\r
-        http://www.springframework.org/schema/tx \r
-        http://www.springframework.org/schema/tx/spring-tx.xsd\r
-        http://www.springframework.org/schema/jee\r
-        http://www.springframework.org/schema/jee/spring-jee.xsd">\r
-\r
-       \r
-       <!-- \r
-               In this way we can not use all the available options for H2.\r
-               \r
-               Because of that I will be using the dataSource bean where I can choose as many options\r
-               as I want for H2.\r
-    <jdbc:embedded-database id="dataSource" type="H2" />\r
-       -->\r
-       \r
-       <!-- \r
-               1. There are two beans with the same id "dataSource"\r
-                  One declared in datasource-configuration.xml\r
-                  Another one declared in datasource-test-configuration.xml\r
-               2. Both beans are declared in different XML files.\r
-               3. Because there are in different XML files Spring does not complain about having duplicate beans.\r
-               4. Because files in src/test will be loaded in class path after files in src/main this bean will\r
-                  override the one declared in datasource-configuration.xml when running JUnit Tests :)\r
-        -->\r
-       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">\r
-               <property name="driverClassName" value="org.h2.Driver" />\r
-               <property name="url" value="jdbc:h2:mem:mybatis_example;INIT=create schema if not exists mybatis_example\;SET SCHEMA mybatis_example;MODE=DB2;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" />\r
-               <property name="username" value="mybatis" />\r
-               <property name="password" value="" />\r
-       </bean>\r
-       \r
-</beans>\r