Skip to content

Commit

Permalink
Added initializing app in development mode by using ServiceConnection…
Browse files Browse the repository at this point in the history
… - PostgresInitializer.kt has been removed
  • Loading branch information
kamil.jedrzejuk committed Oct 8, 2024
1 parent 2ee37f5 commit 3b7448a
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 134 deletions.
7 changes: 5 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dependencies {
implementation("io.github.openfeign:feign-jackson:12.4")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:3.1.2")
implementation("org.postgresql:postgresql:42.3.1")
implementation("org.testcontainers:postgresql:1.20.2")
implementation("org.flywaydb:flyway-core")

// Spring Boot Test
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand All @@ -74,7 +74,10 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.wiremock:wiremock:3.9.1")
testImplementation("org.testcontainers:junit-jupiter:1.20.2")
testImplementation("org.flywaydb:flyway-core")
testImplementation("org.testcontainers:testcontainers:1.20.2")
testImplementation("org.springframework.boot:spring-boot-testcontainers")
testImplementation("org.testcontainers:postgresql:1.20.2")

testRuntimeOnly("org.postgresql:postgresql")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package camilyed.github.io.currencyexchangeapi

import camilyed.github.io.CurrencyExchangeApiApplication
import camilyed.github.io.currencyexchangeapi.testing.config.DevelopmentTimeConfig
import org.springframework.boot.builder.SpringApplicationBuilder

fun main(args: Array<String>) {
SpringApplicationBuilder()
.sources(CurrencyExchangeApiApplication::class.java, DevelopmentTimeConfig::class.java)
.profiles("test")
.run(*args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package camilyed.github.io.currencyexchangeapi.testing

import camilyed.github.io.CurrencyExchangeApiApplication
import camilyed.github.io.currencyexchangeapi.testing.abilties.MakeRequestAbility
import camilyed.github.io.currencyexchangeapi.testing.postgres.PostgresInitializer
import camilyed.github.io.currencyexchangeapi.testing.config.POSTGRES_SQL_CONTAINER
import camilyed.github.io.currencyexchangeapi.testing.utils.DatabaseCleaner
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
Expand All @@ -13,13 +13,13 @@ import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource

@ContextConfiguration(
initializers = [PostgresInitializer::class],
classes = [CurrencyExchangeApiApplication::class],
)
@ActiveProfiles("test")
Expand All @@ -43,7 +43,11 @@ class BaseIntegrationTest : MakeRequestAbility {
}

companion object {
protected val wireMock = WireMockServer(0)

@ServiceConnection
val postgresSQLContainer = POSTGRES_SQL_CONTAINER

private val wireMock = WireMockServer(0)

@JvmStatic
@DynamicPropertySource
Expand All @@ -53,24 +57,55 @@ class BaseIntegrationTest : MakeRequestAbility {

@JvmStatic
@BeforeAll
fun startWireMock() {
fun start() {
startWiremock()
startPostgresContainer()
}

private fun startWiremock() {
if (!wireMock.isRunning) {
println("Starting Wiremock...")
wireMock.start()
WireMock.configureFor(wireMock.port())
println("WIREMOCK STARTED at port: ${wireMock.port()}")
println("Wiremock started at port: ${wireMock.port()}")
}
}

private fun startPostgresContainer() {
if (!postgresSQLContainer.isRunning) {
println("Starting PostgresContainer...")
postgresSQLContainer.start()
println("PostgresContainer started")
}
}

init {
addShutdownHooks()
}

private fun addShutdownHooks() {
Runtime.getRuntime().addShutdownHook(
Thread {
if (wireMock.isRunning) {
println("Shutting down WireMock server...")
wireMock.stop()
println("WireMock stopped")
}
shutdownWiremock()
shutdownPostgresContainer()
},
)
}

private fun shutdownWiremock() {
if (wireMock.isRunning) {
println("Shutting down WireMock server...")
wireMock.stop()
println("WireMock stopped")
}
}

private fun shutdownPostgresContainer() {
if (postgresSQLContainer.isRunning) {
println("Shutting down PostgresContainer...")
postgresSQLContainer.stop()
println("PostgresContainer stopped")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package camilyed.github.io.currencyexchangeapi.testing.config

import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean

@TestConfiguration(proxyBeanMethods = false)
class DevelopmentTimeConfig {

@Bean
@ServiceConnection
fun postgresSQLContainer() = POSTGRES_SQL_CONTAINER
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package camilyed.github.io.currencyexchangeapi.testing.config

import org.testcontainers.containers.PostgreSQLContainer

val POSTGRES_SQL_CONTAINER = PostgreSQLContainer("postgres:13.4-alpine")
.apply {
withDatabaseName("test_db")
withUsername("test_user")
withPassword("test_password")
withInitScript("init.sql")
}

This file was deleted.

6 changes: 0 additions & 6 deletions src/integrationTest/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@

spring:
datasource:
url: jdbc:postgresql://localhost:5432/currency_exchange_db
username: myuser
password: mypassword
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package camilyed.github.io

import camilyed.github.io.currencyexchangeapi.infrastructure.PostgresInitializer
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.ImportAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.cloud.openfeign.FeignAutoConfiguration

Expand All @@ -13,7 +12,5 @@ import org.springframework.cloud.openfeign.FeignAutoConfiguration
class CurrencyExchangeApiApplication

fun main(args: Array<String>) {
val application = SpringApplication(CurrencyExchangeApiApplication::class.java)
application.addInitializers(PostgresInitializer())
application.run(*args)
runApplication<CurrencyExchangeApiApplication>(*args)
}

This file was deleted.

This file was deleted.

0 comments on commit 3b7448a

Please sign in to comment.