Skip to content

Commit

Permalink
Update gradle 8.8 (#53)
Browse files Browse the repository at this point in the history
* Update gradle 8.8

* Update MyApplicationIntegrationTest.kt

* MockBean reset

* MockBean reset

* Fix tests

* Fix things
  • Loading branch information
rogervinas authored Jun 19, 2024
1 parent d21d376 commit 9c48bea
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
HELP.md
.kotlin
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
Expand Down
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,20 @@ class MyApplicationIntegrationTest {
@MockBean // We mock MyEventConsumer
lateinit var eventConsumer: MyEventConsumer
@Test
fun `consume event`() {
@Test
fun `should consume event`() {
val eventCaptor = argumentCaptor<MyEvent>()
doNothing().`when`(eventConsumer).consume(eventCaptor.capture())

// We send a Kafka message using a helper
val text = "hello ${UUID.randomUUID()}"
kafkaProducerHelper.send(TOPIC, "{\"number\":${text.length},\"string\":\"$text\"}")

// We wait at most 5 seconds to receive the expected MyEvent in MyEventConsumer mock
val eventCaptor = argumentCaptor<MyEvent>()
verify(eventConsumer, timeout(FIVE_SECONDS.toMillis())).consume(eventCaptor.capture())

assertThat(eventCaptor.firstValue).satisfies { event ->
assertThat(event.text).isEqualTo(text)
}
}
await().atMost(TEN_SECONDS).untilAsserted {
assertThat(eventCaptor.allValues.filter { it.text == text }).isEqualTo(ONE)
}
}
}
```
* Check the complete test in [MyApplicationIntegrationTest.kt](src/test/kotlin/com/rogervinas/stream/MyApplicationIntegrationTest.kt).
Expand Down Expand Up @@ -278,7 +278,7 @@ spring:
And we can test it like this:
```kotlin
@Test
fun `produce event`() {
fun `should produce event`() {
val text = "hello ${UUID.randomUUID()}"
eventProducer.produce(MyEvent(text))

Expand Down Expand Up @@ -326,19 +326,18 @@ spring:
And we can test it like this:
```kotlin
@Test
fun `retry consume event 5 times`() {
fun `should retry consume event 5 times`() {
// we throw a MyRetryableException every time we receive a message
doThrow(MyRetryableException("retry later!")).`when`(eventConsumer).consume(any())
val eventCaptor = argumentCaptor<MyEvent>()
doThrow(MyRetryableException("retry later!")).`when`(eventConsumer).consume(eventCaptor.capture())

// we send a Kafka message using a helper
val text = "hello ${UUID.randomUUID()}"
kafkaProducerHelper.send(TOPIC, "{\"number\":${text.length},\"string\":\"$text\"}")

// consumer has been called five times with the same message
val eventCaptor = argumentCaptor<MyEvent>()
verify(eventConsumer, timeout(TEN_SECONDS.toMillis()).times(FIVE)).consume(eventCaptor.capture())
assertThat(eventCaptor.allValues).allSatisfy { event ->
assertThat(event.text).isEqualTo(text)
await().atMost(TEN_SECONDS).untilAsserted {
assertThat(eventCaptor.allValues.filter { it.text == text }).isEqualTo(FIVE)
}
}
```
Expand Down Expand Up @@ -384,7 +383,7 @@ And we can test it like this:
#### Application errors:
```kotlin
@Test
fun `send to DLQ rejected messages`() {
fun `should send to DLQ rejected messages`() {
// we throw a MyRetryableException every time we receive a message
doThrow(MyRetryableException("retry later!")).`when`(eventConsumer).consume(any())

Expand Down
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ plugins {

group = "com.rogervinas"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_21
java.targetCompatibility = JavaVersion.VERSION_21

repositories {
mavenCentral()
Expand All @@ -21,6 +19,12 @@ repositories {
val springCloudVersion = "2023.0.2"
val testContainersVersion = "1.19.8"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-starter-stream-kafka")
Expand All @@ -35,6 +39,7 @@ dependencies {

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("com.nhaarman:mockito-kotlin:1.6.0")
testImplementation("org.awaitility:awaitility:4.2.1")
}

dependencyManagement {
Expand All @@ -44,9 +49,8 @@ dependencyManagement {
}

tasks.withType<KotlinCompile> {
kotlinOptions {
compilerOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "21"
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
package com.rogervinas.stream

import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.argumentCaptor
import com.nhaarman.mockito_kotlin.doThrow
import com.nhaarman.mockito_kotlin.timeout
import com.nhaarman.mockito_kotlin.verify
import com.rogervinas.stream.helper.DockerComposeContainerHelper
import com.rogervinas.stream.helper.KafkaConsumerHelper
import com.rogervinas.stream.helper.KafkaProducerHelper
import com.nhaarman.mockito_kotlin.*
import com.rogervinas.stream.domain.MyEvent
import com.rogervinas.stream.domain.MyEventConsumer
import com.rogervinas.stream.domain.MyEventProducer
import com.rogervinas.stream.domain.MyRetryableException
import com.rogervinas.stream.helper.DockerComposeContainerHelper
import com.rogervinas.stream.helper.KafkaConsumerHelper
import com.rogervinas.stream.helper.KafkaProducerHelper
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.Awaitility.await
import org.awaitility.Durations.TEN_SECONDS
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.mockito.Mockito.reset
import org.skyscreamer.jsonassert.JSONAssert
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.boot.test.mock.mockito.MockReset
import org.springframework.test.context.ActiveProfiles
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import java.time.Duration
import java.util.UUID
import java.util.*
import java.util.function.Consumer

@SpringBootTest(webEnvironment = NONE)
Expand All @@ -41,7 +38,7 @@ class MyApplicationIntegrationTest {
private const val TOPIC = "my.topic"
private const val TOPIC_DLQ = "my.topic.errors"

private val TEN_SECONDS = Duration.ofSeconds(10)
private const val ONE = 1
private const val FIVE = 5

@Container
Expand All @@ -52,7 +49,7 @@ class MyApplicationIntegrationTest {
@Qualifier("myStreamEventProducer") // Avoid SpringBootTest issue: expected single matching bean but found 2
lateinit var eventProducer: MyEventProducer

@MockBean
@MockBean(reset = MockReset.BEFORE)
lateinit var eventConsumer: MyEventConsumer

@Value("\${spring.cloud.stream.kafka.binder.brokers}")
Expand All @@ -63,7 +60,6 @@ class MyApplicationIntegrationTest {

@BeforeEach
fun setUp() {
reset(eventConsumer)
kafkaConsumerHelper = KafkaConsumerHelper(kafkaBroker, TOPIC)
kafkaConsumerHelper.consumeAll()
kafkaDLQConsumerHelper = KafkaConsumerHelper(kafkaBroker, TOPIC_DLQ)
Expand All @@ -86,26 +82,30 @@ class MyApplicationIntegrationTest {

@Test
fun `should consume event`() {
val eventCaptor = argumentCaptor<MyEvent>()
doNothing().`when`(eventConsumer).consume(eventCaptor.capture())

val text = "hello ${UUID.randomUUID()}"
kafkaProducerHelper.send(TOPIC, "{\"number\":${text.length},\"string\":\"$text\"}")

val eventCaptor = argumentCaptor<MyEvent>()
verify(eventConsumer, timeout(TEN_SECONDS.toMillis())).consume(eventCaptor.capture())
verify(eventConsumer, timeout(TEN_SECONDS.toMillis())).consume(any())

assertThat(eventCaptor.firstValue).satisfies(Consumer { event -> assertThat(event.text).isEqualTo(text) })
await().atMost(TEN_SECONDS).untilAsserted {
assertThat(eventCaptor.allValues.filter { it.text == text }).hasSize(ONE)
}
}

@Test
fun `should retry consume event 5 times`() {
doThrow(MyRetryableException("retry later!")).`when`(eventConsumer).consume(any())
val eventCaptor = argumentCaptor<MyEvent>()
doThrow(MyRetryableException("retry later!")).`when`(eventConsumer).consume(eventCaptor.capture())

val text = "hello ${UUID.randomUUID()}"
kafkaProducerHelper.send(TOPIC, "{\"number\":${text.length},\"string\":\"$text\"}")

val eventCaptor = argumentCaptor<MyEvent>()
verify(eventConsumer, timeout(TEN_SECONDS.toMillis()).times(FIVE)).consume(eventCaptor.capture())

assertThat(eventCaptor.allValues).allSatisfy(Consumer { event -> assertThat(event.text).isEqualTo(text) })
await().atMost(TEN_SECONDS).untilAsserted {
assertThat(eventCaptor.allValues.filter { it.text == text }).hasSize(FIVE)
}
}

@Test
Expand Down

0 comments on commit 9c48bea

Please sign in to comment.