
Langchain4j Testing Strategies
Add JUnit patterns for LangChain4j streaming chat models so LLM-backed Java services fail fast in CI.
Overview
Langchain4j-testing-strategies is an agent skill for the Ship phase that provides Java/JUnit patterns for testing LangChain4j streaming chat responses.
Install
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill langchain4j-testing-strategiesWhat is this skill?
- StreamingChatModel tests with StreamingChatResponseHandler callbacks
- Partial chunk collection plus CompletableFuture completion and error paths
- OllamaStreamingChatModel example against local baseUrl
- JUnit 5 test class structure for LangChain4j chat responses
- Patterns for asserting streamed chunks and final ChatResponse
Adoption & trust: 1.1k installs on skills.sh; 271 GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your LangChain4j streaming integration works in a demo but you lack reliable tests for partial tokens, completion, and error handling.
Who is it for?
Java backends using LangChain4j with StreamingChatModel who want integration-style tests against Ollama or similar endpoints.
Skip if: Non-Java stacks, prompt-tuning-only work, or production monitoring—use other skills for observability and ops.
When should I use this skill?
When implementing or hardening LangChain4j streaming chat clients and you need repeatable JUnit test patterns.
What do I get? / Deliverables
You can drop in handler-based streaming tests that validate chunk assembly and terminal ChatResponse behavior before release.
- JUnit test classes for streaming ChatResponse behavior
Recommended Skills
Journey fit
How it compares
Test harness recipes for LangChain4j—not a model host, MCP server, or PRD planner.
Common Questions / FAQ
Who is langchain4j-testing-strategies for?
Solo builders and small teams shipping Java LLM features with LangChain4j who need Ship-phase automated tests.
When should I use langchain4j-testing-strategies?
During Ship testing when adding or refactoring streaming chat clients, before merging agent API changes.
Is langchain4j-testing-strategies safe to install?
Example tests may call localhost model URLs—check repo trust and the Security Audits panel on this Prism page before use.
SKILL.md
READMESKILL.md - Langchain4j Testing Strategies
# Advanced Testing Patterns ## Testing Streaming Responses ### Streaming Response Test ```java import dev.langchain4j.model.chat.StreamingChatModel; import dev.langchain4j.model.chat.response.ChatResponse; import dev.langchain4j.model.chat.response.StreamingChatResponseHandler; import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; class StreamingResponseTest { @Test void shouldHandleStreamingResponse() throws Exception { // Arrange StreamingChatModel streamingModel = OllamaStreamingChatModel.builder() .baseUrl("http://localhost:11434") .modelName("llama2") .build(); List<String> chunks = new ArrayList<>(); CompletableFuture<ChatResponse> responseFuture = new CompletableFuture<>(); StreamingChatResponseHandler handler = new StreamingChatResponseHandler() { @Override public void onPartialResponse(String partialResponse) { chunks.add(partialResponse); } @Override public void onComplete(ChatResponse completeResponse) { responseFuture.complete(completeResponse); } @Override public void onError(Throwable error) { responseFuture.completeExceptionally(error); } }; // Act streamingModel.generate("Count to 5", handler); ChatResponse response = responseFuture.get(30, java.util.concurrent.TimeUnit.SECONDS); // Assert assertNotNull(response); assertFalse(chunks.isEmpty()); assertTrue(response.content().text().length() > 0); } } ``` ### Mock Streaming Test ```java @Test void shouldMockStreamingResponse() { // Arrange StreamingChatModel mockModel = mock(StreamingChatModel.class); List<String> chunks = new ArrayList<>(); doAnswer(invocation -> { StreamingChatResponseHandler handler = invocation.getArgument(1); handler.onPartialResponse("Hello "); handler.onPartialResponse("World"); handler.onComplete(Response.from(AiMessage.from("Hello World"))); return null; }).when(mockModel) .generate(anyString(), any(StreamingChatResponseHandler.class)); // Act mockModel.generate("Test", new StreamingChatResponseHandler() { @Override public void onPartialResponse(String partialResponse) { chunks.add(partialResponse); } @Override public void onComplete(ChatResponse response) {} @Override public void onError(Throwable error) {} }); // Assert assertEquals(2, chunks.size()); assertEquals("Hello World", String.join("", chunks)); } ``` ## Memory Management Testing ### Chat Memory Testing ```java import dev.langchain4j.memory.chat.MessageWindowChatMemory; class MemoryTest { @Test void testChatMemory() { // Arrange var memory = MessageWindowChatMemory.withMaxMessages(3); memory.add(UserMessage.from("Message 1")); memory.add(AiMessage.from("Response 1")); memory.add(UserMessage.from("Message 2")); memory.add(AiMessage.from("Response 2")); // Assert List<ChatMessage> messages = memory.messages(); assertEquals(4, messages.size()); // Add more to test window memory.add(UserMessage.from("Message 3")); assertEquals(4, memory.messages().size()); // Window size limit } @Test void testMultiUserMemory() { var memoryProvider = memoryId -> MessageWindowChatMemory.withMaxMessages(10); var memory1 = memoryProvider.provide("user1"); var memory2 = memoryProvider.provide("user2"); memory1.add(UserMessage.from("User 1 message")); memory2.add(UserMessage.from("User 2 message")); assertEquals(1,