Skip to content

Commit ebb5d6a

Browse files
author
m.zharinova
committed
add application kt exclusion from test report
1 parent be912c3 commit ebb5d6a

File tree

9 files changed

+136
-48
lines changed

9 files changed

+136
-48
lines changed

spring-boot-3-demo-app-kotlin/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ dependencies {
4444
testImplementation("org.springframework.cloud:spring-cloud-starter-contract-stub-runner")
4545
}
4646

47+
val coverageExcludeList = mutableListOf(
48+
"**/*ApplicationKt.class"
49+
)
50+
listOf(JacocoCoverageVerification::class, JacocoReport::class).forEach { taskType ->
51+
tasks.withType(taskType) {
52+
afterEvaluate {
53+
classDirectories.setFrom(
54+
files(
55+
classDirectories.files.map { file ->
56+
fileTree(file).apply {
57+
exclude(coverageExcludeList)
58+
}
59+
}
60+
)
61+
)
62+
}
63+
}
64+
}
4765
springBoot {
4866
buildInfo()
4967
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Licensed under the Apache License 2.0
66
*/
77

8-
package io.github.mfvanek.spring.boot3.kotlin.test
8+
package io.github.mfvanek.spring.boot3.kotlin.test.controllers
99

1010
import org.springframework.web.bind.annotation.GetMapping
1111
import org.springframework.web.bind.annotation.RestController
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Licensed under the Apache License 2.0
66
*/
77

8-
package io.github.mfvanek.spring.boot3.kotlin.test
8+
package io.github.mfvanek.spring.boot3.kotlin.test.controllers
99

1010
import java.net.URI
1111
import org.springframework.http.HttpHeaders
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
/*
2-
* Copyright (c) 2020-2025. Ivan Vakhrushev and others.
3-
* https://github.com/mfvanek/spring-boot-open-telemetry-demo
4-
*
5-
* Licensed under the Apache License 2.0
6-
*/
7-
package io.github.mfvanek.spring.boot3.kotlin.test
8-
9-
import io.github.mfvanek.spring.boot3.kotlin.test.service.KafkaSendingService
10-
import io.github.mfvanek.spring.boot3.kotlin.test.service.PublicApiService
11-
import io.github.oshai.kotlinlogging.KotlinLogging
12-
import io.micrometer.tracing.Tracer
13-
import java.time.Clock
14-
import java.time.LocalDateTime
15-
import org.springframework.web.bind.annotation.GetMapping
16-
import org.springframework.web.bind.annotation.RestController
17-
18-
private val logger = KotlinLogging.logger {}
19-
20-
@RestController
21-
class TimeController(
22-
private val tracer: Tracer,
23-
private val clock: Clock,
24-
private val kafkaSendingService: KafkaSendingService,
25-
private val publicApiService: PublicApiService
26-
) {
27-
28-
@GetMapping(path = ["/current-time"])
29-
fun getNow(): LocalDateTime {
30-
logger.trace { "tracer $tracer" }
31-
val traceId = tracer.currentSpan()?.context()?.traceId()
32-
logger.info { "Called method getNow. TraceId = $traceId" }
33-
val nowFromRemote = publicApiService.getZonedTime()
34-
val now = nowFromRemote ?: LocalDateTime.now(clock)
35-
kafkaSendingService.sendNotification("Current time = $now")
36-
.thenRun { logger.info{"Awaiting acknowledgement from Kafka"} }
37-
.get()
38-
return now
39-
}
40-
}
1+
/*
2+
* Copyright (c) 2020-2025. Ivan Vakhrushev and others.
3+
* https://github.com/mfvanek/spring-boot-open-telemetry-demo
4+
*
5+
* Licensed under the Apache License 2.0
6+
*/
7+
8+
package io.github.mfvanek.spring.boot3.kotlin.test.controllers
9+
10+
import io.github.mfvanek.spring.boot3.kotlin.test.service.KafkaSendingService
11+
import io.github.mfvanek.spring.boot3.kotlin.test.service.PublicApiService
12+
import io.github.oshai.kotlinlogging.KotlinLogging
13+
import io.micrometer.tracing.Tracer
14+
import java.time.Clock
15+
import java.time.LocalDateTime
16+
import org.springframework.web.bind.annotation.GetMapping
17+
import org.springframework.web.bind.annotation.RestController
18+
19+
private val logger = KotlinLogging.logger {}
20+
@RestController
21+
class TimeController(
22+
private val tracer: Tracer,
23+
private val clock: Clock,
24+
private val kafkaSendingService: KafkaSendingService,
25+
private val publicApiService: PublicApiService
26+
) {
27+
28+
@GetMapping(path = ["/current-time"])
29+
fun getNow(): LocalDateTime {
30+
logger.trace { "tracer $tracer" }
31+
val traceId = tracer.currentSpan()?.context()?.traceId()
32+
logger.info { "Called method getNow. TraceId = $traceId" }
33+
val nowFromRemote = publicApiService.getZonedTime()
34+
val now = nowFromRemote ?: LocalDateTime.now(clock)
35+
kafkaSendingService.sendNotification("Current time = $now")
36+
.thenRun { logger.info{"Awaiting acknowledgement from Kafka"} }
37+
.get()
38+
return now
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.mfvanek.spring.boot3.kotlin.test.controllers
2+
3+
import io.github.mfvanek.spring.boot3.kotlin.test.filters.TraceIdInResponseServletFilter.Companion.TRACE_ID_HEADER_NAME
4+
import io.github.mfvanek.spring.boot3.kotlin.test.support.TestBase
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
class HomeControllerTest : TestBase() {
9+
@Test
10+
fun homeControllerShouldWork() {
11+
val result = webTestClient.get()
12+
.uri("/")
13+
.exchange()
14+
.expectStatus().isEqualTo(200)
15+
.expectHeader().exists(TRACE_ID_HEADER_NAME)
16+
.expectBody(String::class.java)
17+
.returnResult()
18+
.responseBody;
19+
assertThat<String>(result)
20+
.isEqualTo("Hello!")
21+
}
22+
}

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/controllers/RedirectControllerTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package io.github.mfvanek.spring.boot3.kotlin.test.controllers
22

33
import io.github.mfvanek.spring.boot3.kotlin.test.filters.TraceIdInResponseServletFilter.Companion.TRACE_ID_HEADER_NAME
44
import io.github.mfvanek.spring.boot3.kotlin.test.support.TestBase
5-
import org.assertj.core.api.Assertions
5+
import org.assertj.core.api.Assertions.assertThat
66
import org.junit.jupiter.api.Test
77

8-
class RedirectControllerTest: TestBase() {
8+
class RedirectControllerTest : TestBase() {
99
@Test
10-
fun redirectShouldWork(){
10+
fun redirectShouldWork() {
1111
val result = webTestClient.get()
1212
.uri("/redirect")
1313
.exchange()
@@ -17,7 +17,7 @@ class RedirectControllerTest: TestBase() {
1717
.expectBody(Any::class.java)
1818
.returnResult()
1919
.responseBody;
20-
Assertions.assertThat<Any>(result)
20+
assertThat<Any>(result)
2121
.isNull()
2222
}
2323
}

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/controllers/TimeControllerTest.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.mfvanek.spring.boot3.kotlin.test.support.TestBase
77
import java.nio.charset.StandardCharsets
88
import java.time.Duration
99
import java.time.LocalDateTime
10+
import java.time.temporal.ChronoUnit
1011
import java.util.Locale
1112
import java.util.UUID
1213
import java.util.concurrent.BlockingQueue
@@ -18,6 +19,8 @@ import org.apache.kafka.clients.consumer.ConsumerRecord
1819
import org.apache.kafka.common.config.SaslConfigs
1920
import org.apache.kafka.common.serialization.UUIDDeserializer
2021
import org.assertj.core.api.Assertions.assertThat
22+
import org.assertj.core.api.Assertions.within
23+
import org.assertj.core.data.TemporalUnitOffset
2124
import org.junit.jupiter.api.AfterAll
2225
import org.junit.jupiter.api.BeforeAll
2326
import org.junit.jupiter.api.BeforeEach
@@ -109,9 +112,9 @@ class TimeControllerTest : TestBase() {
109112
.expectHeader().exists(TRACE_ID_HEADER_NAME)
110113
.expectBody(LocalDateTime::class.java)
111114
.returnResult()
112-
val traceId = result.responseHeaders.getFirst(TRACE_ID_HEADER_NAME);
115+
val traceId = result.responseHeaders.getFirst(TRACE_ID_HEADER_NAME)
113116
assertThat(traceId)
114-
.isEqualTo("38c19768104ab8ae64fabbeed65bbbdf");
117+
.isEqualTo("38c19768104ab8ae64fabbeed65bbbdf")
115118
assertThat(output.all)
116119
.containsPattern(
117120
String.format(
@@ -134,6 +137,25 @@ class TimeControllerTest : TestBase() {
134137

135138
}
136139

140+
@Order(3)
141+
@Test
142+
fun currentTimeReceivedFromClockWhenRemoteServiceGivesBadResponse() {
143+
stubErrorResponse()
144+
145+
val localDateTime = LocalDateTime.now(clock)
146+
val result = webTestClient.get().uri { uriBuilder -> uriBuilder.path("current-time").build() }
147+
.header("traceparent", "00-38c19768104ab8ae64fabbeed65bbbdf-4cac1747d4e1ee10-01")
148+
.exchange()
149+
.expectStatus().isOk()
150+
.expectHeader().exists(TRACE_ID_HEADER_NAME)
151+
.expectBody(LocalDateTime::class.java)
152+
.returnResult()
153+
154+
assertThat(result).isNotNull
155+
assertThat(result.responseBody).isCloseTo(localDateTime, within(5, ChronoUnit.SECONDS))
156+
}
157+
158+
137159
private fun countRecordsInTable(): Long {
138160
val queryResult = jdbcTemplate.queryForObject("select count(*) from otel_demo.storage", Long::class.java)
139161
return queryResult ?: 0L

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/service/PublicApiServiceTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ class PublicApiServiceTest : TestBase() {
9393
)
9494
)
9595
}
96+
@Test
97+
fun throwsJsonProcessingExceptionWithBdResponse(output: CapturedOutput) {
98+
stubBadResponse()
99+
Observation.createNotStarted("test", observationRegistry).observe {
100+
val result = publicApiService.getZonedTime()
101+
assertThat(result).isNull()
102+
assertThat(tracer.currentSpan()?.context()?.traceId()).isNotNull()
103+
assertThat(output.all).contains("Failed to convert response")
104+
}
105+
}
96106
}

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/support/TestBase.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ abstract class TestBase {
6868
return zoneName
6969
}
7070

71+
protected fun stubBadResponse(): String {
72+
val zoneName = TimeZone.getDefault().id
73+
stubBadResponse(zoneName)
74+
return zoneName
75+
}
76+
7177
private fun stubErrorResponse(zoneName: String, errorForResponse: RuntimeException) {
7278
WireMock.stubFor(
7379
WireMock.get(WireMock.urlPathMatching("/$zoneName"))
@@ -78,4 +84,14 @@ abstract class TestBase {
7884
)
7985
)
8086
}
87+
private fun stubBadResponse(zoneName: String) {
88+
WireMock.stubFor(
89+
WireMock.get(WireMock.urlPathMatching("/$zoneName"))
90+
.willReturn(
91+
WireMock.aResponse()
92+
.withStatus(200)
93+
.withBody(objectMapper.writeValueAsString("Bad response"))
94+
)
95+
)
96+
}
8197
}

0 commit comments

Comments
 (0)