Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@

<modules>
<module>springboot-modules</module>
<module>testContainers</module>
</modules>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven-pmd.version>3.25.0</maven-pmd.version>
<maven-surefire-plugin.version>3.5.0</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
Expand All @@ -112,6 +111,7 @@
<junit-jupiter.version>5.10.3</junit-jupiter.version>
<junit-jupiter-engine.version>5.10.3</junit-jupiter-engine.version>
<hsqldb.version>2.7.3</hsqldb.version>
<testcontainers.version>1.21.1</testcontainers.version>
</properties>

</project>
3 changes: 1 addition & 2 deletions springboot-modules/spring-ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@
</repositories>-->

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

@SpringBootTest
@ActiveProfiles("sa")
public class AdvisorUnitTest {
public class AdvisorLiveTest {

private static final Logger LOGGER = LoggerFactory.getLogger(AdvisorUnitTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AdvisorLiveTest.class);

@Autowired
ChatModel chatModel;
Expand Down
9 changes: 9 additions & 0 deletions testContainers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# **Published Articles**

## **Testcontainers**

This module contains source codes related to the articles on Testcontainers.
- [**Ollama - Testcontainers Module in Java**](https://www.kodesastra.com/2025/06/ollama-testcontainers-module-in-java.html)



53 changes: 53 additions & 0 deletions testContainers/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kodesastra</groupId>
<artifactId>kodesastra_blog</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>testContainers</artifactId>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.21.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>ollama</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
</properties>

</project>
22 changes: 22 additions & 0 deletions testContainers/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="ERROR"/>
<logger name="tc.dockerImageName" level="DEBUG"/>

<logger name="com.github.dockerjava" level="WARN"/>
<logger name="org.zeroturnaround.exec" level="WARN"/>
<logger name="org.testcontainers.shaded" level="WARN"/>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.ks.testcontainers.ollama;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Container;
import org.testcontainers.ollama.OllamaContainer;

public class OllamaContainerLiveTest {
private static final Logger logger = LoggerFactory.getLogger(OllamaContainerLiveTest.class);

static OllamaContainer ollamaContainer;
private static final String OLLAMA_IMAGE = "ollama/ollama:latest";
private static final String LLM = "tinyllama:1.1b";

@BeforeAll
static void init() throws IOException, InterruptedException {

ollamaContainer = new OllamaContainer(OLLAMA_IMAGE);
ollamaContainer.withCreateContainerCmdModifier((cmd) ->
cmd.getHostConfig().withDeviceRequests(null));

ollamaContainer.start();

ollamaContainer.execInContainer("ollama", "pull", LLM);
}

@AfterAll
static void cleanUp() {
if (ollamaContainer != null) {
try {
ollamaContainer.stop();
} catch (Exception e) {
logger.error("Error stopping Ollama container: {}", e.getMessage());
}
}
}

@Test
void test() throws IOException, InterruptedException {
String prompt = """
Context:
The sun is a star located at the center of our solar system.
It provides the Earth with heat and light, making life possible.
The sun is composed mostly of hydrogen and helium
and generates energy through nuclear fusion."
Question: What two gases make up most of the sun?
Instructions:
Please answer strictly from the context provided in the prompt and no other additional
should be provided.Also, keep the answer short and concise."
""";
Container.ExecResult execResult =
ollamaContainer.execInContainer("ollama", "run", LLM, prompt);
assertEquals(0, execResult.getExitCode(), "Exit code should be 0");
logger.info("Exec Result: {}", execResult.getStdout());
}
}
Loading