Skip to content

Commit

Permalink
Merge pull request #13 from CamilYed/develop
Browse files Browse the repository at this point in the history
Added initializing app in development mode by using ServiceConnection - PostgresInitializer.kt has been removed
  • Loading branch information
CamilYed authored Oct 8, 2024
2 parents 44da69b + faec879 commit 4443481
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 136 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ In this example, the test is written in a readable DSL style, ensuring clarity a
./gradlew build
```

3. Run the application:
3. Run the application in development mode:
```bash
./gradlew bootRun
./gradlew runDev
```

4. The application will be accessible at `http://localhost:8080`.
Expand Down
17 changes: 15 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 Expand Up @@ -125,3 +128,13 @@ tasks.wrapper {
gradleVersion = "8.10.2"
distributionType = Wrapper.DistributionType.ALL
}

tasks.register<JavaExec>("runDev") {
group = "application"
description = "Run the application with DevelopmentTimeConfig and 'test' profile"
mainClass.set("camilyed.github.io.currencyexchangeapi.TestCurrencyExchangeApiApplicationKt")
classpath = sourceSets["main"].runtimeClasspath + sourceSets["integrationTest"].runtimeClasspath
args = listOf()
jvmArgs = listOf()
environment("SPRING_PROFILES_ACTIVE", "test")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package camilyed.github.io.currencyexchangeapi

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

class TestCurrencyExchangeApiApplication
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 4443481

Please sign in to comment.