Skip to content

Commit 5f324d7

Browse files
garyrussellartembilan
authored andcommitted
Add Kotlin test case
* Use 'kotlin-spring' gradle plugin
1 parent bfccd92 commit 5f324d7

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

build.gradle

+19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
buildscript {
2+
ext.kotlinVersion = '1.2.41'
23
repositories {
34
maven { url 'https://repo.spring.io/plugins-release' }
45
}
56
dependencies {
67
classpath 'io.spring.gradle:docbook-reference-plugin:0.3.1'
78
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
9+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
10+
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
811
}
912
}
1013

@@ -49,14 +52,23 @@ subprojects { subproject ->
4952
apply plugin: 'idea'
5053
apply plugin: 'jacoco'
5154
apply plugin: 'checkstyle'
55+
apply plugin: 'kotlin'
56+
apply plugin: 'kotlin-spring'
5257

5358
compileJava {
5459
sourceCompatibility = 1.8
5560
targetCompatibility = 1.8
5661
}
5762

63+
compileTestKotlin {
64+
kotlinOptions {
65+
jvmTarget = '1.8'
66+
}
67+
}
68+
5869
ext {
5970
assertjVersion = '3.9.1'
71+
assertkVersion = '0.10'
6072
hamcrestVersion = '1.3'
6173
jacksonVersion = '2.9.5'
6274
jaywayJsonPathVersion = '2.4.0'
@@ -94,6 +106,13 @@ subprojects { subproject ->
94106
testCompileOnly 'org.apiguardian:apiguardian-api:1.0.0'
95107

96108
testRuntime "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"
109+
110+
testCompile("com.willowtreeapps.assertk:assertk:$assertkVersion") {
111+
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
112+
}
113+
114+
testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
115+
testRuntime "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
97116
}
98117

99118
// enable all compiler warnings; individual projects may customize further
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2016-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import org.apache.kafka.clients.consumer.ConsumerConfig
18+
import org.apache.kafka.clients.producer.ProducerConfig
19+
import org.apache.kafka.common.serialization.StringDeserializer
20+
import org.apache.kafka.common.serialization.StringSerializer
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.junit.jupiter.api.Test
23+
import org.springframework.beans.factory.annotation.Autowired
24+
import org.springframework.beans.factory.annotation.Value
25+
import org.springframework.context.annotation.Bean
26+
import org.springframework.context.annotation.Configuration
27+
import org.springframework.kafka.annotation.EnableKafka
28+
import org.springframework.kafka.annotation.KafkaListener
29+
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
30+
import org.springframework.kafka.core.*
31+
import org.springframework.kafka.test.context.EmbeddedKafka
32+
import org.springframework.kafka.test.rule.KafkaEmbedded
33+
import org.springframework.test.annotation.DirtiesContext
34+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
35+
import java.util.concurrent.CountDownLatch
36+
import java.util.concurrent.TimeUnit
37+
38+
39+
/**
40+
* @author Gary Russell
41+
* @since 2.2
42+
*/
43+
44+
@SpringJUnitConfig
45+
@DirtiesContext
46+
@EmbeddedKafka(topics = ["kotlinTestTopic"])
47+
class EnableKafkaKotlinTests {
48+
49+
@Autowired
50+
private lateinit var config: Config
51+
52+
@Autowired
53+
private lateinit var template: KafkaTemplate<String, String>
54+
55+
@Test
56+
fun `test listener`() {
57+
this.template.send("kotlinTestTopic", "foo")
58+
assertThat(this.config.latch.await(10, TimeUnit.SECONDS)).isTrue()
59+
assertThat(this.config.received).isEqualTo("foo")
60+
}
61+
62+
@Configuration
63+
@EnableKafka
64+
class Config {
65+
66+
lateinit var received: String
67+
68+
val latch = CountDownLatch(1)
69+
70+
@Value("\${" + KafkaEmbedded.SPRING_EMBEDDED_KAFKA_BROKERS + "}")
71+
private lateinit var brokerAddresses: String
72+
73+
@Bean
74+
fun kpf(): ProducerFactory<String, String> {
75+
val configs = HashMap<String, Any>()
76+
configs[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = this.brokerAddresses
77+
configs[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
78+
configs[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
79+
return DefaultKafkaProducerFactory(configs)
80+
}
81+
82+
@Bean
83+
fun kcf(): ConsumerFactory<String, String> {
84+
val configs = HashMap<String, Any>()
85+
configs["foo"] = "bar"
86+
configs[ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG] = this.brokerAddresses
87+
configs[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java
88+
configs[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java
89+
configs[ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG] = false
90+
configs[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
91+
return DefaultKafkaConsumerFactory(configs)
92+
}
93+
94+
@Bean
95+
fun kt(): KafkaTemplate<String, String> {
96+
return KafkaTemplate(kpf())
97+
}
98+
99+
@Bean
100+
fun kafkaListenerContainerFactory(): ConcurrentKafkaListenerContainerFactory<String, String> {
101+
val factory: ConcurrentKafkaListenerContainerFactory<String, String>
102+
= ConcurrentKafkaListenerContainerFactory()
103+
factory.consumerFactory = kcf()
104+
return factory
105+
}
106+
107+
@KafkaListener(id = "kotlin", topics = ["kotlinTestTopic"])
108+
fun listen(value: String) {
109+
this.received = value
110+
this.latch.countDown()
111+
}
112+
113+
}
114+
115+
}

0 commit comments

Comments
 (0)