Skip to content

Commit b0911c1

Browse files
committed
Introduce Kotlin tests
This commit configures Maven to support compiling Kotlin main and test classes, and adds a JacksonUtilsKotlinTests class that ensures that serialization and deserialization of Kotlin data classes works as expected. The Maven configuration follows recommendations from https://kotlinlang.org/docs/maven.html.
1 parent c02d136 commit b0911c1

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<azure-open-ai-client.version>1.0.0-beta.10</azure-open-ai-client.version>
170170
<jtokkit.version>1.1.0</jtokkit.version>
171171
<victools.version>4.31.1</victools.version>
172+
<kotlin.version>1.9.25</kotlin.version>
172173

173174
<!-- NOTE: keep them align -->
174175
<bedrockruntime.version>2.26.7</bedrockruntime.version>
@@ -313,6 +314,37 @@
313314
<artifactId>maven-site-plugin</artifactId>
314315
<version>${maven-site-plugin.version}</version>
315316
</plugin>
317+
<plugin>
318+
<groupId>org.jetbrains.kotlin</groupId>
319+
<artifactId>kotlin-maven-plugin</artifactId>
320+
<version>${kotlin.version}</version>
321+
<executions>
322+
<execution>
323+
<id>compile</id>
324+
<goals>
325+
<goal>compile</goal>
326+
</goals>
327+
<configuration>
328+
<sourceDirs>
329+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
330+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
331+
</sourceDirs>
332+
</configuration>
333+
</execution>
334+
<execution>
335+
<id>test-compile</id>
336+
<goals>
337+
<goal>test-compile</goal>
338+
</goals>
339+
<configuration>
340+
<sourceDirs>
341+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
342+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
343+
</sourceDirs>
344+
</configuration>
345+
</execution>
346+
</executions>
347+
</plugin>
316348
<plugin>
317349
<groupId>org.apache.maven.plugins</groupId>
318350
<artifactId>maven-compiler-plugin</artifactId>
@@ -323,6 +355,32 @@
323355
<compilerArg>-parameters</compilerArg>
324356
</compilerArgs>
325357
</configuration>
358+
<executions>
359+
<!-- Replacing default-compile as it is treated specially by Maven -->
360+
<execution>
361+
<id>default-compile</id>
362+
<phase>none</phase>
363+
</execution>
364+
<!-- Replacing default-testCompile as it is treated specially by Maven -->
365+
<execution>
366+
<id>default-testCompile</id>
367+
<phase>none</phase>
368+
</execution>
369+
<execution>
370+
<id>java-compile</id>
371+
<phase>compile</phase>
372+
<goals>
373+
<goal>compile</goal>
374+
</goals>
375+
</execution>
376+
<execution>
377+
<id>java-test-compile</id>
378+
<phase>test-compile</phase>
379+
<goals>
380+
<goal>testCompile</goal>
381+
</goals>
382+
</execution>
383+
</executions>
326384
</plugin>
327385
<plugin>
328386
<groupId>org.apache.maven.plugins</groupId>

spring-ai-core/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@
140140
<scope>test</scope>
141141
</dependency>
142142

143+
<dependency>
144+
<groupId>org.jetbrains.kotlin</groupId>
145+
<artifactId>kotlin-stdlib</artifactId>
146+
<version>${kotlin.version}</version>
147+
<scope>test</scope>
148+
</dependency>
149+
150+
<dependency>
151+
<groupId>com.fasterxml.jackson.module</groupId>
152+
<artifactId>jackson-module-kotlin</artifactId>
153+
<version>${jackson.version}</version>
154+
<scope>test</scope>
155+
</dependency>
156+
143157
</dependencies>
144158

145159
<profiles>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023-2024 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+
* https://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 com.fasterxml.jackson.databind.json.JsonMapper
18+
import org.assertj.core.api.Assertions
19+
import org.junit.jupiter.api.Test
20+
import org.springframework.ai.util.JacksonUtils
21+
22+
/**
23+
* Kotlin unit tests for [JacksonUtils].
24+
*
25+
* @author Sebastien Deleuze
26+
*/
27+
class JacksonUtilsKotlinTests {
28+
29+
@Test
30+
fun `Deserialize to a Kotlin data class with Jackson modules detected by JacksonUtils#instantiateAvailableModules`() {
31+
val jsonMapper = JsonMapper.builder().addModules(JacksonUtils.instantiateAvailableModules()).build()
32+
val output = jsonMapper.readValue("{\"name\":\"Robert\",\"age\":42}", User::class.java)
33+
Assertions.assertThat(output).isEqualTo(User("Robert", 42))
34+
}
35+
36+
@Test
37+
fun `Serialize a Kotlin data class with Jackson modules detected by JacksonUtils#instantiateAvailableModules`() {
38+
val jsonMapper = JsonMapper.builder().addModules(JacksonUtils.instantiateAvailableModules()).build()
39+
val output = jsonMapper.writeValueAsString(User("Robert", 42))
40+
Assertions.assertThat(output).isEqualTo("{\"name\":\"Robert\",\"age\":42}")
41+
}
42+
43+
data class User(val name: String, val age: Int)
44+
45+
}

0 commit comments

Comments
 (0)