Added example repository test using Postgres 9.4.15 through testcontainers

This commit is contained in:
Reinhard Prechtl 2017-11-25 13:34:30 +01:00
parent 152e2553e1
commit 7e55f006f8
7 changed files with 103 additions and 3 deletions

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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<MyEntity, Long> {
}

View file

@ -0,0 +1 @@
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL94Dialect

View file

@ -0,0 +1,16 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.testcontainers" level="INFO"/>
<logger name="org.apache.http" level="WARN"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="org.zeroturnaround.exec" level="WARN"/>
</configuration>

View file

@ -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();
}
}

View file

@ -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