> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 单元测试容器

> 在 Spring AI 中使用 Testcontainers

# Testcontainers

Spring AI 提供 Testcontainers 支持，用于测试您的 AI 应用程序。

## 概述

Testcontainers 允许您在 Docker 容器中针对真实服务运行测试，提供更真实的测试环境。

## 特性

* AI 模型的预配置容器
* 向量数据库容器
* 与 Spring Boot 测试的轻松集成
* 自动容器生命周期管理

## 开始使用

将以下依赖项添加到您的项目中：

```xml theme={"system"}
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-testcontainers</artifactId>
    <scope>test</scope>
</dependency>
```

## 使用方法

```java theme={"system"}
@SpringBootTest
class MyAITest {

    @Container
    static OllamaContainer ollama = new OllamaContainer();

    @Test
    void testWithOllama() {
        // 在此处编写测试代码
    }
}
```

## 可用容器

* OllamaContainer
* MilvusContainer
* QdrantContainer
* WeaviateContainer
* ChromaContainer

## 配置

可以通过属性或编程方式配置容器：

```java theme={"system"}
@Container
static OllamaContainer ollama = new OllamaContainer()
    .withModel("llama2")
    .withPort(11434);
```

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/spring-ai/api/testcontainers.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
