Skip to content

Commit

Permalink
Fixing test-suite docs examples to use test-resources (#825)
Browse files Browse the repository at this point in the history
* Fixing test-suite docs examples to use test-resources, except for MyTest used by docs as example for using testcontainers directly.

* Cleanup abstract class and add logging

---------

Co-authored-by: Tim Yates <tim.yates@gmail.com>
  • Loading branch information
wetted and timyates authored Aug 18, 2023
1 parent 1281d47 commit 3777f31
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ tasks.withType(Test) {
micronaut {
importMicronautPlatform.set(false)
testResources {
sharedServer = true
additionalModules.add("kafka")
}
}
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testcontainers = "1.18.3"
zipkin-brave-kafka-clients = '5.16.0'

micronaut-cache = "4.0.2"
micronaut-logging = "1.0.0"
micronaut-micrometer = "5.0.1"
micronaut-reactor = "3.0.1"
micronaut-rxjava2 = "2.0.1"
Expand All @@ -26,7 +27,6 @@ micronaut-serde = "2.2.0"
micronaut-tracing = "5.0.1"
micronaut-test = "4.0.0"

logback = "1.4.6"


[libraries]
Expand Down Expand Up @@ -56,4 +56,3 @@ micronaut-tracing = { module = "io.micronaut.tracing:micronaut-tracing-bom", ver
junit-jupiter-engine = { module = 'org.junit.jupiter:junit-jupiter-engine' }

micronaut-gradle-plugin = { module = "io.micronaut.gradle:micronaut-gradle-plugin", version.ref = "micronaut-gradle-plugin" }

1 change: 1 addition & 0 deletions test-suite-groovy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
testCompileOnly(mn.micronaut.inject.groovy)
testImplementation(libs.testcontainers.kafka)
testImplementation(mnTest.micronaut.test.spock)
testRuntimeOnly(mnLogging.logback.classic)
testRuntimeOnly(libs.junit.jupiter.engine)
testImplementation(mnReactor.micronaut.reactor)
testImplementation(mnSerde.micronaut.serde.jackson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package io.micronaut.kafka.docs.quickstart

import io.micronaut.context.BeanContext
import io.micronaut.context.annotation.Property
import io.micronaut.kafka.docs.AbstractKafkaTest
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@Property(name = 'spec.name', value = 'QuickStartTest')
@Property(name = 'kafka.enabled', value = 'true')
@MicronautTest
class QuickStartTest extends AbstractKafkaTest {
class QuickStartTest extends Specification {

@Inject
BeanContext beanContext
Expand Down
1 change: 1 addition & 0 deletions test-suite-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
testImplementation(libs.testcontainers.kafka)
testImplementation(mnTest.micronaut.test.junit5)
testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(mnLogging.logback.classic)
testImplementation(libs.awaitility)
testImplementation(mnReactor.micronaut.reactor)
testImplementation(mnSerde.micronaut.serde.jackson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.micronaut.kafka.docs
import io.micronaut.test.support.TestPropertyProvider
import org.testcontainers.containers.KafkaContainer
import org.testcontainers.utility.DockerImageName
import java.util.*

/**
* @see <a href="https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers">Singleton containers</a>
Expand All @@ -12,18 +11,14 @@ abstract class AbstractKafkaTest : TestPropertyProvider {

companion object {
var MY_KAFKA: KafkaContainer = KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"))

init {
MY_KAFKA.start()
}
}

override fun getProperties(): MutableMap<String, String> {
while(!MY_KAFKA.isRunning()) {
if(!MY_KAFKA.isRunning) {
MY_KAFKA.start()
}
return Collections.singletonMap(
"kafka.bootstrap.servers", MY_KAFKA.getBootstrapServers()
return mutableMapOf(
"kafka.bootstrap.servers" to MY_KAFKA.bootstrapServers
)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package io.micronaut.kafka.docs

import io.micronaut.configuration.kafka.annotation.*
import io.micronaut.context.annotation.*
import io.micronaut.configuration.kafka.annotation.KafkaClient
import io.micronaut.configuration.kafka.annotation.KafkaListener
import io.micronaut.configuration.kafka.annotation.OffsetReset
import io.micronaut.configuration.kafka.annotation.Topic
import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.awaitility.Awaitility.await
import org.junit.jupiter.api.*
import java.util.concurrent.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.util.concurrent.TimeUnit

@Property(name = "spec.name", value = "MyTest")
@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class MyTest : AbstractKafkaTest() {

@Test
fun testKafkaRunning(producer: MyProducer, consumer: MyConsumer) {
val message = "hello"
producer.produce(message)
await().atMost(5, TimeUnit.SECONDS)
.until { consumer.consumed == message }
MY_KAFKA.stop()
await().atMost(5, TimeUnit.SECONDS).until { consumer.consumed == message }
}

@Requires(property = "spec.name", value = "MyTest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package io.micronaut.kafka.docs.quickstart

import io.micronaut.context.BeanContext
import io.micronaut.context.annotation.Property
import io.micronaut.kafka.docs.AbstractKafkaTest
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@Property(name = "spec.name", value = "QuickstartTest")
@Property(name = "kafka.enabled", value = "true")
@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class QuickstartTest : AbstractKafkaTest() {
internal class QuickstartTest {

@Inject
lateinit var beanContext: BeanContext

Expand All @@ -21,7 +20,5 @@ internal class QuickstartTest : AbstractKafkaTest() {
val client = beanContext.getBean(ProductClient::class.java)
client.sendProduct("Nike", "Blue Trainers")
// end::quickstart[]

MY_KAFKA.stop()
}
}
1 change: 1 addition & 0 deletions test-suite/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies {
testImplementation(libs.testcontainers.kafka)
testImplementation(mnTest.micronaut.test.junit5)
testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(mnLogging.logback.classic)
testImplementation(libs.awaitility)
testImplementation(mnReactor.micronaut.reactor)
testImplementation(mnSerde.micronaut.serde.jackson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.*;
import java.util.Collections;
import java.util.Map;

/**
* @see <a href="https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers">Singleton containers</a>
*/
public abstract class AbstractKafkaTest implements TestPropertyProvider {

static protected final KafkaContainer MY_KAFKA;

static {
MY_KAFKA = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:latest"));
MY_KAFKA.start();
}
static protected final KafkaContainer MY_KAFKA = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:latest")
);

@Override
public Map<String, String> getProperties() {
while (!MY_KAFKA.isRunning()) {
if (!MY_KAFKA.isRunning()) {
MY_KAFKA.start();
}
return Collections.singletonMap(
Expand Down
12 changes: 8 additions & 4 deletions test-suite/src/test/java/io/micronaut/kafka/docs/MyTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.micronaut.kafka.docs;

import io.micronaut.configuration.kafka.annotation.*;
import io.micronaut.context.annotation.*;
import io.micronaut.configuration.kafka.annotation.KafkaClient;
import io.micronaut.configuration.kafka.annotation.KafkaListener;
import io.micronaut.configuration.kafka.annotation.OffsetReset;
import io.micronaut.configuration.kafka.annotation.Topic;
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
Expand All @@ -18,7 +23,6 @@ void testKafkaRunning(MyProducer producer, MyConsumer consumer) {
final String message = "hello";
producer.produce(message);
await().atMost(5, SECONDS).until(() -> message.equals(consumer.consumed));
MY_KAFKA.stop();
}

@Requires(property = "spec.name", value = "MyTest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import io.micronaut.kafka.docs.AbstractKafkaTest;

@Property(name = "spec.name", value = "QuickstartTest")
@Property(name = "kafka.enabled", value = "true")
@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class QuickstartTest extends AbstractKafkaTest {
class QuickstartTest {
@Inject
ApplicationContext applicationContext;

Expand All @@ -21,7 +19,5 @@ void testSendProduct() {
ProductClient client = applicationContext.getBean(ProductClient.class);
client.sendProduct("Nike", "Blue Trainers");
// end::quickstart[]

MY_KAFKA.stop();
}
}

0 comments on commit 3777f31

Please sign in to comment.