$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-testing-strategiesProvides unit test, integration test, and mock AI patterns for LangChain4j applications. Creates mock LLM responses, tests retrieval chains, validates RAG workflows, and implements Testcontainers-based integration tests for Java AI services. Use when unit testing AI services, int
| 1 | # LangChain4J Testing Strategies |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Patterns for unit testing with mocks, integration testing with Testcontainers, and end-to-end validation of RAG systems, AI Services, and tool execution. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - **Unit testing AI services**: When you need fast, isolated tests for services using LangChain4j AiServices |
| 10 | - **Integration testing LangChain4j components**: When testing real ChatModel, EmbeddingModel, or RAG pipelines with Testcontainers |
| 11 | - **Mocking AI models**: When you need deterministic responses without calling external APIs |
| 12 | - **Testing LLM-based Java applications**: When validating RAG workflows, tool execution, or retrieval chains |
| 13 | |
| 14 | ## Instructions |
| 15 | |
| 16 | ### 1. Unit Testing with Mocks |
| 17 | |
| 18 | Use mock models for fast, isolated testing. See `references/unit-testing.md`. |
| 19 | |
| 20 | ```java |
| 21 | ChatModel mockModel = mock(ChatModel.class); |
| 22 | when(mockModel.generate(any(String.class))) |
| 23 | .thenReturn(Response.from(AiMessage.from("Mocked response"))); |
| 24 | |
| 25 | var service = AiServices.builder(AiService.class) |
| 26 | .chatModel(mockModel) |
| 27 | .build(); |
| 28 | ``` |
| 29 | |
| 30 | ### 2. Configure Testing Dependencies |
| 31 | |
| 32 | Setup Maven/Gradle dependencies. See `references/testing-dependencies.md`. |
| 33 | |
| 34 | - `langchain4j-test` - Guardrail assertions |
| 35 | - `testcontainers` - Containerized testing |
| 36 | - `mockito` - Mock external dependencies |
| 37 | - `assertj` - Fluent assertions |
| 38 | |
| 39 | ### 3. Integration Testing with Testcontainers |
| 40 | |
| 41 | Test with real services. See `references/integration-testing.md`. |
| 42 | |
| 43 | ```java |
| 44 | @Testcontainers |
| 45 | class OllamaIntegrationTest { |
| 46 | @Container |
| 47 | static GenericContainer<?> ollama = new GenericContainer<>( |
| 48 | DockerImageName.parse("ollama/ollama:0.5.4") |
| 49 | ).withExposedPorts(11434); |
| 50 | |
| 51 | @Test |
| 52 | void shouldGenerateResponse() { |
| 53 | // Verify container is healthy |
| 54 | assertTrue(ollama.isRunning()); |
| 55 | await().atMost(30, TimeUnit.SECONDS) |
| 56 | .until(() -> ollama.getLogs().contains("API server listening")); |
| 57 | |
| 58 | ChatModel model = OllamaChatModel.builder() |
| 59 | .baseUrl(ollama.getEndpoint()) |
| 60 | .build(); |
| 61 | |
| 62 | // Verify model responds before running tests |
| 63 | assertDoesNotThrow(() -> model.generate("ping")); |
| 64 | |
| 65 | String response = model.generate("Test query"); |
| 66 | assertNotNull(response); |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | ### 4. Advanced Features |
| 72 | |
| 73 | Streaming, memory, error handling patterns in `references/advanced-testing.md`. |
| 74 | |
| 75 | ### 5. Testing Workflow |
| 76 | |
| 77 | Follow the testing pyramid from `references/workflow-patterns.md`: |
| 78 | |
| 79 | - **70% Unit Tests**: Fast, isolated with mocks |
| 80 | - **20% Integration Tests**: Real services with health checks |
| 81 | - **10% End-to-End Tests**: Complete workflows |
| 82 | |
| 83 | ``` |
| 84 | 70% Unit Tests ─ Mock ChatModel, guardrails, edge cases |
| 85 | 20% Integration Tests ─ Testcontainers, vector stores, RAG |
| 86 | 10% End-to-End Tests ─ Complete user journeys |
| 87 | ``` |
| 88 | |
| 89 | ### Troubleshooting |
| 90 | |
| 91 | - **Container fails to start**: Check Docker daemon is running, verify image exists, increase timeout |
| 92 | - **Model not responding**: Verify baseUrl is correct, check container logs, ensure model is loaded |
| 93 | - **Test timeout**: Increase `@Timeout` duration for slow models, check container resource limits |
| 94 | - **Flaky tests**: Add retry logic or health checks before assertions |
| 95 | |
| 96 | ## Examples |
| 97 | |
| 98 | ### Unit Test |
| 99 | |
| 100 | ```java |
| 101 | @Test |
| 102 | void shouldProcessQueryWithMock() { |
| 103 | ChatModel mockModel = mock(ChatModel.class); |
| 104 | when(mockModel.generate(any(String.class))) |
| 105 | .thenReturn(Response.from(AiMessage.from("Test response"))); |
| 106 | |
| 107 | var service = AiServices.builder(AiService.class) |
| 108 | .chatModel(mockModel) |
| 109 | .build(); |
| 110 | |
| 111 | String result = service.chat("What is Java?"); |
| 112 | assertEquals("Test response", result); |
| 113 | } |
| 114 | ``` |
| 115 | |
| 116 | ### Integration Test with Testcontainers |
| 117 | |
| 118 | ```java |
| 119 | @Testcontainers |
| 120 | class RAGIntegrationTest { |
| 121 | @Container |
| 122 | static GenericContainer<?> ollama = new GenericContainer<>( |
| 123 | DockerImageName.parse("ollama/ollama:0.5.4") |
| 124 | ); |
| 125 | |
| 126 | @BeforeAll |
| 127 | static void waitForContainerReady() { |
| 128 | await().atMost(60, TimeUnit.SECONDS) |
| 129 | .until(() -> ollama.getLogs().contains("API server listening")); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | void shouldCompleteRAGWorkflow() { |
| 134 | assertTrue(ollama.isRunning()); |
| 135 | |
| 136 | var chatModel = OllamaChatModel.builder() |
| 137 | .baseUrl(ollama.getEndpoint()) |
| 138 | .build(); |
| 139 | |
| 140 | var embeddingModel = OllamaEmbeddingModel.builder() |
| 141 | .baseUrl(ollama.getEndpoint()) |
| 142 | .build(); |
| 143 | |
| 144 | var store = new InMemoryEmbeddingStore<>(); |
| 145 | var retriever = EmbeddingStoreContentRetriever.bu |