Skip to content

Commit 73a4598

Browse files
committed
add spring-boot deps runtime check
1 parent 2022204 commit 73a4598

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

starters/spring/stove-spring-testing-e2e-kafka/src/main/kotlin/com/trendyol/stove/testing/e2e/kafka/Options.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ internal fun TestSystem.kafka(): KafkaSystem = getOrNone<KafkaSystem>().getOrEls
229229
fun WithDsl.kafka(
230230
configure: () -> KafkaSystemOptions
231231
): TestSystem {
232+
SpringKafkaVersionCheck.ensureSpringKafkaAvailable()
232233
val options = configure()
233234

234235
val runtime: SystemRuntime = if (options is ProvidedKafkaSystemOptions) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.trendyol.stove.testing.e2e.kafka
2+
3+
/**
4+
* Utility object to check Spring Kafka availability at runtime.
5+
* Since Spring Kafka is a `compileOnly` dependency, users must bring their own version.
6+
*/
7+
internal object SpringKafkaVersionCheck {
8+
private const val KAFKA_TEMPLATE_CLASS = "org.springframework.kafka.core.KafkaTemplate"
9+
10+
/**
11+
* Checks if Spring Kafka is available on the classpath.
12+
* @throws IllegalStateException if Spring Kafka is not found
13+
*/
14+
fun ensureSpringKafkaAvailable() {
15+
try {
16+
Class.forName(KAFKA_TEMPLATE_CLASS)
17+
} catch (e: ClassNotFoundException) {
18+
throw IllegalStateException(
19+
"""
20+
|
21+
|═══════════════════════════════════════════════════════════════════════════════
22+
| Spring Kafka Not Found on Classpath!
23+
|═══════════════════════════════════════════════════════════════════════════════
24+
|
25+
| stove-spring-testing-e2e-kafka requires Spring Kafka to be on your classpath.
26+
| Spring Kafka is declared as a 'compileOnly' dependency, so you must add it
27+
| to your project.
28+
|
29+
| Add one of the following to your build.gradle.kts:
30+
|
31+
| For Spring Boot 2.x:
32+
| testImplementation("org.springframework.kafka:spring-kafka:2.9.x")
33+
| // or use the starter:
34+
| testImplementation("org.springframework.boot:spring-boot-starter-kafka:2.7.x")
35+
|
36+
| For Spring Boot 3.x:
37+
| testImplementation("org.springframework.kafka:spring-kafka:3.x.x")
38+
| // or use the starter:
39+
| testImplementation("org.springframework.boot:spring-boot-starter-kafka:3.x.x")
40+
|
41+
| For Spring Boot 4.x:
42+
| testImplementation("org.springframework.kafka:spring-kafka:4.x.x")
43+
| // or use the starter:
44+
| testImplementation("org.springframework.boot:spring-boot-starter-kafka:4.x.x")
45+
|
46+
|═══════════════════════════════════════════════════════════════════════════════
47+
""".trimMargin(),
48+
e
49+
)
50+
}
51+
}
52+
}

starters/spring/stove-spring-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/SpringApplicationUnderTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ internal fun TestSystem.systemUnderTest(
2323
fun WithDsl.springBoot(
2424
runner: Runner<ConfigurableApplicationContext>,
2525
withParameters: List<String> = listOf()
26-
): ReadyTestSystem = this.testSystem.systemUnderTest(runner, withParameters)
26+
): ReadyTestSystem {
27+
SpringBootVersionCheck.ensureSpringBootAvailable()
28+
return this.testSystem.systemUnderTest(runner, withParameters)
29+
}
2730

2831
@StoveDsl
2932
class SpringApplicationUnderTest(
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
@file:Suppress("TooGenericExceptionCaught", "SwallowedException")
2+
3+
package com.trendyol.stove.testing.e2e
4+
5+
/**
6+
* Utility object to check Spring Boot availability and version at runtime.
7+
* Since Spring Boot is a `compileOnly` dependency, users must bring their own version.
8+
*/
9+
internal object SpringBootVersionCheck {
10+
private const val SPRING_APPLICATION_CLASS = "org.springframework.boot.SpringApplication"
11+
private const val SPRING_BOOT_VERSION_CLASS = "org.springframework.boot.SpringBootVersion"
12+
13+
/**
14+
* Checks if Spring Boot is available on the classpath.
15+
* @throws IllegalStateException if Spring Boot is not found
16+
*/
17+
fun ensureSpringBootAvailable() {
18+
try {
19+
Class.forName(SPRING_APPLICATION_CLASS)
20+
} catch (e: ClassNotFoundException) {
21+
throw IllegalStateException(
22+
"""
23+
|
24+
|═══════════════════════════════════════════════════════════════════════════════
25+
| Spring Boot Not Found on Classpath!
26+
|═══════════════════════════════════════════════════════════════════════════════
27+
|
28+
| stove-spring-testing-e2e requires Spring Boot to be on your classpath.
29+
| Spring Boot is declared as a 'compileOnly' dependency, so you must add it
30+
| to your project.
31+
|
32+
| Add one of the following to your build.gradle.kts:
33+
|
34+
| For Spring Boot 2.x:
35+
| testImplementation("org.springframework.boot:spring-boot-starter:2.7.x")
36+
|
37+
| For Spring Boot 3.x:
38+
| testImplementation("org.springframework.boot:spring-boot-starter:3.x.x")
39+
|
40+
| For Spring Boot 4.x:
41+
| testImplementation("org.springframework.boot:spring-boot-starter:4.x.x")
42+
|
43+
|═══════════════════════════════════════════════════════════════════════════════
44+
""".trimMargin(),
45+
e
46+
)
47+
}
48+
}
49+
50+
/**
51+
* Gets the Spring Boot version if available.
52+
* @return the Spring Boot version string, or "unknown" if not determinable
53+
*/
54+
fun getSpringBootVersion(): String = try {
55+
val versionClass = Class.forName(SPRING_BOOT_VERSION_CLASS)
56+
val getVersionMethod = versionClass.getMethod("getVersion")
57+
getVersionMethod.invoke(null) as? String ?: "unknown"
58+
} catch (_: Exception) {
59+
"unknown"
60+
}
61+
62+
/**
63+
* Gets the major version of Spring Boot.
64+
* @return the major version (2, 3, 4, etc.) or -1 if not determinable
65+
*/
66+
fun getSpringBootMajorVersion(): Int = try {
67+
val version = getSpringBootVersion()
68+
version.split(".").firstOrNull()?.toIntOrNull() ?: -1
69+
} catch (e: Exception) {
70+
-1
71+
}
72+
}

0 commit comments

Comments
 (0)