langchain4j-testing-strategies
Provides 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, integration testing Lan
By giuseppe-trisciuoglio · 2,886 installs
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-testing-strategies
Source repository · Upstream listing
LangChain4J Testing Strategies
Overview
Patterns for unit testing with mocks, integration testing with Testcontainers, and end to end validation of RAG systems, AI Services, and tool execution.
When to Use
Unit testing AI services : When you need fast, isolated tests for services using LangChain4j AiServices
Integration testing LangChain4j components : When testing real ChatModel, EmbeddingModel, or RAG pipelines with Testcontainers
Mocking AI models : When you need deterministic responses without calling external APIs
Testing LLM based Java applications : When validating RAG workflows, tool execution, or retrieval chains
Instructions
1. Unit Testing with Mocks
Use mock models for fast, isolated testing. See references/unit testing.md .
2. Configure Testing Dependencies
Setup Maven/Gradle dependencies. See references/testing dependencies.md .
langchain4j test Guardrail assertions
testcontainers Containerized testing
mockito Mock external dependencies
assertj Fluent assertions
3. Integration Testing with Testcontainers
Test with real services. See references/integration testing.md .
4. Advanced Features
Streaming, memory, error handling patterns in references/advanced testing.md .
5. Testing Workflow
Follow the testing pyramid from references/workflow patterns.md :
70% Unit Tests : Fast, isolated with mocks
20% Integration Tests : Real services with health checks
10% End to End Tests : Complete workflows
Troubleshooting
Container fails to start : Check Docker daemon is running, verify image exists, increase timeout
Model not responding : Verify baseUrl is correct, check container logs, ensure model is loaded
Test timeout : Increase @Timeout duration for slow models, check container resource limits
Flaky tests : Add retry logic or health checks before assertions
Examples
Unit Test
Integration Test with Testcontainers
Best Practices
Use @BeforeEach / @AfterEach for test isolation
Never call real APIs in unit tests; use mocks
Include @Timeout for external service calls
Test both success and error handling scenarios
Validate response coherence and edge cases
Common Patterns
Mock Strategy
Assertion Helpers
Reference Documentation
[Testing Dependencies](references/testing dependencies.md) Maven/Gradle configuration
[Unit Testing](references/unit testing.md) Mock models, guardrails
[Integration Testing](references/integration testing.md) Testcontainers, real services
[Advanced Testing](references/advanced testing.md) Streaming, memory, error handling
[Workflow Patterns](references/workflow patterns.md) Test pyramid, best practices
Constraints and Warnings
AI responses are non deterministic; use mocks for reliable unit tests
Avoid real API calls in tests to prevent costs and rate limiting
Integration tests require Docker; use container health checks
RAG tests need properly seeded embedding stores
Mock based tests cannot guarantee actual LLM behavior; supplement with integration tests
Use test specific configuration profiles; never affect production data