Skip to content

Commit 2b07f35

Browse files
Fix Micronaut annotation processing
The security-grpc and hikari-health projects were ported from a project that has annotation processing set up. The annotation processing was not transferred.
1 parent 9752903 commit 2b07f35

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

hikari-health/build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
plugins {
22
`kotlin-conventions`
33
`publish-conventions`
4+
alias(libs.plugins.ksp)
45
alias(libs.plugins.micronaut.library)
6+
alias(libs.plugins.kotlin.allOpen)
57
}
68

79
dependencies {
@@ -11,6 +13,19 @@ dependencies {
1113
implementation(mn.micronaut.jdbc.hikari)
1214
implementation(mn.reactor)
1315

16+
ksp(mn.micronaut.inject.kotlin)
17+
1418
testImplementation(libs.bundles.mockito)
19+
testImplementation(mn.micronaut.test.junit5)
1520
testRuntimeOnly(mn.h2)
21+
22+
kspTest(mn.micronaut.inject.kotlin)
23+
}
24+
25+
allOpen {
26+
preset("micronaut")
27+
}
28+
29+
micronaut {
30+
testRuntime("junit5")
1631
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.pkware.micronaut.health.hikari
2+
3+
import assertk.assertThat
4+
import assertk.assertions.isFalse
5+
import assertk.assertions.isTrue
6+
import io.micronaut.context.ApplicationContext
7+
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
8+
import jakarta.inject.Inject
9+
import org.junit.jupiter.api.Test
10+
11+
/**
12+
* Verifies that [HikariHealthIndicator] is registered as a Micronaut bean under the right
13+
* conditions. These tests would fail if KSP annotation processing were absent — the runtime
14+
* logic tests in [HikariHealthIndicatorTest] cannot catch that.
15+
*
16+
* The datasource is configured in `application-test.properties`, which is only loaded when
17+
* the `"test"` environment is active (i.e. via [MicronautTest]). Manual [ApplicationContext.run]
18+
* calls do NOT activate the test environment, so they start without any datasource — enabling
19+
* clean testing of the [io.micronaut.context.annotation.Requires] conditions.
20+
*/
21+
@MicronautTest(startApplication = false)
22+
class HikariHealthIndicatorBeanTest {
23+
24+
@Inject
25+
lateinit var context: ApplicationContext
26+
27+
@Test
28+
fun `registers as a bean when Hikari datasource is configured`() {
29+
assertThat(context.containsBean(HikariHealthIndicator::class.java)).isTrue()
30+
}
31+
32+
@Test
33+
fun `not registered when disabled via property`() {
34+
// Provide the datasource so @Requires(beans=[DataSource]) is satisfied; only the
35+
// property flag should prevent registration.
36+
ApplicationContext.run(
37+
mapOf(
38+
"endpoints.health.jdbc.hikari.enabled" to "false",
39+
"datasources.default.url" to "jdbc:h2:mem:hikari_disabled;DB_CLOSE_DELAY=-1",
40+
"datasources.default.driver-class-name" to "org.h2.Driver",
41+
"datasources.default.username" to "sa",
42+
"datasources.default.password" to "",
43+
),
44+
).use { ctx ->
45+
assertThat(ctx.containsBean(HikariHealthIndicator::class.java)).isFalse()
46+
}
47+
}
48+
49+
@Test
50+
fun `not registered when no DataSource is configured`() {
51+
// No datasource properties → @Requires(beans=[DataSource::class]) is not satisfied.
52+
// deduceEnvironment(false) prevents Micronaut from auto-activating the "test" environment
53+
// (which it detects via micronaut-test-junit5 on the classpath), ensuring application-test
54+
// .properties is not loaded and no datasource auto-configuration takes place.
55+
ApplicationContext.builder().deduceEnvironment(false).build().start().use { ctx ->
56+
assertThat(ctx.containsBean(HikariHealthIndicator::class.java)).isFalse()
57+
}
58+
}
59+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
datasources.default.url=jdbc:h2:mem:hikari_bean_test;DB_CLOSE_DELAY=-1
2+
datasources.default.driver-class-name=org.h2.Driver
3+
datasources.default.username=sa
4+
datasources.default.password=

security-grpc/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
plugins {
22
`java-conventions`
33
`publish-conventions`
4+
alias(libs.plugins.ksp)
45
alias(libs.plugins.micronaut.library)
6+
alias(libs.plugins.kotlin.allOpen)
57
}
68

79
dependencies {
@@ -20,10 +22,16 @@ dependencies {
2022
}
2123
implementation(mn.reactor)
2224

25+
ksp(mn.micronaut.inject.kotlin)
26+
2327
testAnnotationProcessor(mn.micronaut.inject.java)
2428
// SecuredAnnotationMapper SPI must be on the annotation processor classpath so that
2529
// @Secured on test beans generates @Executable metadata for GrpcSecuredMethodRegistry.
2630
testAnnotationProcessor(projects.securityGrpcProcessor)
2731
testImplementation(mn.micronaut.http.server)
2832
testImplementation(mn.micronaut.test.junit5)
2933
}
34+
35+
allOpen {
36+
preset("micronaut")
37+
}

0 commit comments

Comments
 (0)