Package org.mockserver.client
Class TurnBuilder
java.lang.Object
org.mockserver.client.TurnBuilder
Sub-builder for configuring a single turn within an LLM conversation mock.
Provides fluent predicates for matching inbound requests and a response
to return when all predicates match. Returns to the parent
LlmConversationBuilder via andThen().
-
Method Summary
Modifier and TypeMethodDescriptionandThen()Return to the parent conversation builder for chaining additional turns.applyTo(MockServerClient client) Shortcut: build and register all turns with the MockServerClient.respondingWith(Completion completion) Set the completion to return for this turn.whenContainsToolResultFor(String toolName) Match when the conversation contains a tool result for a prior call of the named tool.Match when the latest message's text content contains the given substring.whenLatestMessageContains(Pattern regex) Deprecated.misleading name — this overload matches by regex, not substring.whenLatestMessageMatches(Pattern regex) Match when the latest message's text content matches the given regex.Match when the latest message has the given role.whenSemanticMatch(String expectedMeaning) Opt-in fuzzy semantic match: match when the latest message expresses the given intent, judged by a runtime LLM.whenTurnIndex(int n) Match when the conversation has exactlynassistant turns.withChaos(LlmChaosProfile chaos) Apply a fault/chaos profile to this turn's response (probabilistic errors, mid-stream truncation, malformed SSE) for resilience testing.withNormalization(NormalizationOptions normalization) Apply opt-in prompt normalisation before thewhenLatestMessage…text predicates are evaluated, so cosmetic differences (whitespace, JSON key ordering, volatile ids/timestamps) do not block a match.
-
Method Details
-
whenTurnIndex
Match when the conversation has exactlynassistant turns.- Parameters:
n- the turn index (0-based count of assistant messages)- Returns:
- this builder
-
whenLatestMessageContains
Match when the latest message's text content contains the given substring.- Parameters:
text- the substring to match- Returns:
- this builder
-
whenLatestMessageMatches
Match when the latest message's text content matches the given regex.- Parameters:
regex- the regex pattern to match- Returns:
- this builder
- Throws:
IllegalArgumentException- if the regex fails to compile
-
whenLatestMessageContains
Deprecated.misleading name — this overload matches by regex, not substring. UsewhenLatestMessageMatches(Pattern). Retained for source compatibility; delegates towhenLatestMessageMatches. -
whenLatestMessageRole
Match when the latest message has the given role.- Parameters:
role- the role to match- Returns:
- this builder
-
whenContainsToolResultFor
Match when the conversation contains a tool result for a prior call of the named tool.- Parameters:
toolName- the tool name to look for- Returns:
- this builder
-
whenSemanticMatch
Opt-in fuzzy semantic match: match when the latest message expresses the given intent, judged by a runtime LLM. Off by default and ignored unlessmockserver.llmSemanticMatchingEnabledis set and a backend resolves. Non-deterministic — exploratory only, never for assertions.- Parameters:
expectedMeaning- the intent the latest message should express- Returns:
- this builder
-
withNormalization
Apply opt-in prompt normalisation before thewhenLatestMessage…text predicates are evaluated, so cosmetic differences (whitespace, JSON key ordering, volatile ids/timestamps) do not block a match. Deterministic — the same prompt always normalises identically.- Parameters:
normalization- the normalisation options- Returns:
- this builder
-
withChaos
Apply a fault/chaos profile to this turn's response (probabilistic errors, mid-stream truncation, malformed SSE) for resilience testing.- Parameters:
chaos- the chaos profile- Returns:
- this builder
-
respondingWith
Set the completion to return for this turn.- Parameters:
completion- the completion response- Returns:
- this builder
-
andThen
Return to the parent conversation builder for chaining additional turns.- Returns:
- the parent LlmConversationBuilder
-
applyTo
Shortcut: build and register all turns with the MockServerClient. Delegates to the parent builder'sapplyTo.- Parameters:
client- the MockServerClient- Returns:
- the created expectations
-