From 7e55f006f8b5239c42dd37fb97c9624a6d320bf7 Mon Sep 17 00:00:00 2001 From: Reinhard Prechtl Date: Sat, 25 Nov 2017 13:34:30 +0100 Subject: [PATCH] Added example repository test using Postgres 9.4.15 through testcontainers --- ...TestcontainersPostgresDemoApplication.java | 9 ++-- .../entity/MyEntity.java | 20 +++++++++ .../repository/MyEntityRepository.java | 9 ++++ src/main/resources/application.properties | 1 + src/main/resources/logback.xml | 16 +++++++ .../repository/MyEntityRepositoryTest.java | 45 +++++++++++++++++++ src/test/resources/application.properties | 6 +++ 7 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/rpr/testcontainerspostgresdemo/entity/MyEntity.java create mode 100644 src/main/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepository.java create mode 100644 src/main/resources/logback.xml create mode 100644 src/test/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepositoryTest.java create mode 100644 src/test/resources/application.properties diff --git a/src/main/java/de/rpr/testcontainerspostgresdemo/TestcontainersPostgresDemoApplication.java b/src/main/java/de/rpr/testcontainerspostgresdemo/TestcontainersPostgresDemoApplication.java index bd46760..c9d61e9 100644 --- a/src/main/java/de/rpr/testcontainerspostgresdemo/TestcontainersPostgresDemoApplication.java +++ b/src/main/java/de/rpr/testcontainerspostgresdemo/TestcontainersPostgresDemoApplication.java @@ -1,12 +1,15 @@ package de.rpr.testcontainerspostgresdemo; +import de.rpr.testcontainerspostgresdemo.repository.MyEntityRepository; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication +@EnableJpaRepositories(basePackageClasses = MyEntityRepository.class) public class TestcontainersPostgresDemoApplication { - public static void main(String[] args) { - SpringApplication.run(TestcontainersPostgresDemoApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(TestcontainersPostgresDemoApplication.class, args); + } } diff --git a/src/main/java/de/rpr/testcontainerspostgresdemo/entity/MyEntity.java b/src/main/java/de/rpr/testcontainerspostgresdemo/entity/MyEntity.java new file mode 100644 index 0000000..f84cf8d --- /dev/null +++ b/src/main/java/de/rpr/testcontainerspostgresdemo/entity/MyEntity.java @@ -0,0 +1,20 @@ +package de.rpr.testcontainerspostgresdemo.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +@Data +@NoArgsConstructor +@AllArgsConstructor +public class MyEntity { + + @Id + @GeneratedValue + private Long id; +} diff --git a/src/main/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepository.java b/src/main/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepository.java new file mode 100644 index 0000000..54af08d --- /dev/null +++ b/src/main/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepository.java @@ -0,0 +1,9 @@ +package de.rpr.testcontainerspostgresdemo.repository; + +import de.rpr.testcontainerspostgresdemo.entity.MyEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MyEntityRepository extends JpaRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29..f16eb90 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL94Dialect \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..1315a0f --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepositoryTest.java b/src/test/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepositoryTest.java new file mode 100644 index 0000000..553ad87 --- /dev/null +++ b/src/test/java/de/rpr/testcontainerspostgresdemo/repository/MyEntityRepositoryTest.java @@ -0,0 +1,45 @@ +package de.rpr.testcontainerspostgresdemo.repository; + +import de.rpr.testcontainerspostgresdemo.TestcontainersPostgresDemoApplication; +import de.rpr.testcontainerspostgresdemo.entity.MyEntity; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.context.TestComponent; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.testcontainers.containers.PostgreSQLContainer; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = TestcontainersPostgresDemoApplication.class) +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +public class MyEntityRepositoryTest { + + @Autowired + ApplicationContext applicationContext; + @Autowired + MyEntityRepository repository; + + @ClassRule + public static PostgreSQLContainer postgres = new PostgreSQLContainer(); + + @Test + public void should_load_context() { + assertThat(applicationContext).isNotNull(); + } + + @Test + public void persisted_entity_should_be_loadable() { + repository.save(new MyEntity(1L)); + assertThat(repository.findOne(1L)).isNotNull(); + } +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..d36002a --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,6 @@ +spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver +spring.datasource.url=jdbc:tc:postgresql:9.4.15://localhost:5432/test +spring.datasource.username=test +spring.datasource.password=test + +spring.jpa.hibernate.ddl-auto = create \ No newline at end of file