Merge pull request #7 from TouchInstinct/common-spring-test-jpa
Common spring test jpa
This commit is contained in:
commit
c90c7848a4
|
|
@ -48,3 +48,7 @@
|
|||
## common-spring-test
|
||||
|
||||
Утилиты для тестирования в среде `spring-test`
|
||||
|
||||
## common-spring-test-jpa
|
||||
|
||||
Утилиты для тестирования репозиториев
|
||||
|
|
|
|||
|
|
@ -49,6 +49,13 @@ subprojects {
|
|||
dependency("ch.qos.logback:logback-classic:1.2.3")
|
||||
dependency("ch.qos.logback.contrib:logback-json-classic:0.1.5")
|
||||
dependency("ch.qos.logback.contrib:logback-jackson:0.1.5")
|
||||
|
||||
dependency("org.testcontainers:testcontainers:1.15.1")
|
||||
dependency("org.testcontainers:postgresql:1.15.1")
|
||||
dependency("org.testcontainers:junit-jupiter:1.15.1")
|
||||
dependency("org.junit.jupiter:junit-jupiter-api:5.4.2")
|
||||
dependency("org.junit.jupiter:junit-jupiter-params:5.4.2")
|
||||
dependency("org.junit.jupiter:junit-jupiter-engine:5.4.2")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
plugins {
|
||||
id("kotlin")
|
||||
id("kotlin-spring")
|
||||
id("maven-publish")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||
|
||||
compileOnly(project(":common-spring-jpa"))
|
||||
compileOnly(project(":common-spring-test"))
|
||||
|
||||
compileOnly("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||
compileOnly("org.springframework.boot:spring-boot-starter-test")
|
||||
|
||||
compileOnly("org.testcontainers:testcontainers")
|
||||
compileOnly("org.testcontainers:postgresql")
|
||||
compileOnly("org.testcontainers:junit-jupiter")
|
||||
compileOnly("org.junit.jupiter:junit-jupiter-api")
|
||||
compileOnly("org.junit.jupiter:junit-jupiter-params")
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.test.jpa.repository
|
||||
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
|
||||
import org.springframework.context.annotation.Import
|
||||
import org.springframework.test.context.ActiveProfiles
|
||||
import ru.touchin.common.spring.test.annotations.SlowTest
|
||||
|
||||
@ActiveProfiles("test", "test-slow")
|
||||
@SlowTest
|
||||
@DataJpaTest
|
||||
@Import(RepositoryTestConfiguration::class)
|
||||
annotation class RepositoryTest
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
@file:Suppress("SpringFacetCodeInspection")
|
||||
package ru.touchin.common.spring.test.jpa.repository
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.testcontainers.containers.JdbcDatabaseContainer
|
||||
import org.testcontainers.containers.PostgreSQLContainer
|
||||
import org.testcontainers.containers.wait.strategy.Wait
|
||||
import ru.touchin.common.spring.jpa.EnableJpaAuditingExtra
|
||||
import javax.sql.DataSource
|
||||
|
||||
@TestConfiguration
|
||||
@EnableJpaAuditingExtra
|
||||
@ComponentScan
|
||||
class RepositoryTestConfiguration {
|
||||
|
||||
// запуск и остановка контейнера по lifecycle-событиями компонента (1)
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
fun jdbcDatabaseContainer(
|
||||
@Value("\${tests.slow.db.imageName}") imageName: String,
|
||||
): JdbcDatabaseContainer<*> {
|
||||
return PostgreSQLContainer<Nothing>(imageName).apply {
|
||||
waitingFor(Wait.forListeningPort())
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun dataSource(jdbcDatabaseContainer: JdbcDatabaseContainer<*>): DataSource {
|
||||
val hikariConfig = HikariConfig()
|
||||
.apply {
|
||||
jdbcUrl = jdbcDatabaseContainer.jdbcUrl
|
||||
username = jdbcDatabaseContainer.username
|
||||
password = jdbcDatabaseContainer.password
|
||||
}
|
||||
|
||||
return HikariDataSource(hikariConfig)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
spring:
|
||||
datasource:
|
||||
url: jdbc:postgresql://localhost:5432/test-db
|
||||
username: postgres
|
||||
password: qwerty
|
||||
platform: postgresql
|
||||
initialization-mode: always
|
||||
jpa:
|
||||
databasePlatform: org.hibernate.dialect.PostgreSQL95Dialect
|
||||
liquibase:
|
||||
enabled: true
|
||||
test:
|
||||
database:
|
||||
replace: none
|
||||
tests:
|
||||
slow:
|
||||
db:
|
||||
imageName: "postgres:12"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
spring:
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: none
|
||||
liquibase:
|
||||
enabled: false
|
||||
main:
|
||||
lazy-initialization: true
|
||||
|
|
@ -24,3 +24,4 @@ include("common-spring")
|
|||
include("common-spring-jpa")
|
||||
include("common-spring-web")
|
||||
include("common-spring-test")
|
||||
include("common-spring-test-jpa")
|
||||
|
|
|
|||
Loading…
Reference in New Issue