Class LangChain4jAssertions
java.lang.Object
org.assertj.core.api.Assertions
dev.langchain4j.test.LangChain4jAssertions
- All Implemented Interfaces:
org.assertj.core.api.InstanceOfAssertFactories
The entry point for all LangChain4j assertions, and for all AssertJ assertions, with a single static import of
assertThat. To capture AI Service invocations, also import AiServiceCapture.capture(Callable):
import static dev.langchain4j.test.LangChain4jAssertions.assertThat;
import static dev.langchain4j.test.service.AiServiceCapture.capture;
AiServiceCapture capture = capture(() -> assistant.chat("Cancel booking MS-777"));
assertThat(capture).hasToolExecuted("cancelBooking");
assertThat(capture.outputText()).contains("cancelled");
This works for every return type of the AI Service method, also Result<...>: capture the call, and use
capture.output() for what it returned. These assertions are deterministic: they check what happened,
without calling a model. To check a text with natural-language criteria, using a model as a judge, see
Judge. To run the AI Service in a test without a network, build it on a MockChatModel or a
MockStreamingChatModel that answers what the test tells it to.
Only the assertions on captured AI Service invocations (AiServiceCapture and AiServiceInvocation)
need the langchain4j module, where AI Services are defined: add it with the same version as your other
LangChain4j modules to capture AI Service invocations. The other assertions of this class, for example on a
ChatRequest or a guardrail result, also work without it.
-
Field Summary
Fields inherited from interface org.assertj.core.api.InstanceOfAssertFactories
ARRAY, ARRAY_2D, ATOMIC_BOOLEAN, ATOMIC_INTEGER, ATOMIC_INTEGER_ARRAY, ATOMIC_INTEGER_FIELD_UPDATER, ATOMIC_LONG, ATOMIC_LONG_ARRAY, ATOMIC_LONG_FIELD_UPDATER, ATOMIC_MARKABLE_REFERENCE, ATOMIC_REFERENCE, ATOMIC_REFERENCE_ARRAY, ATOMIC_REFERENCE_FIELD_UPDATER, ATOMIC_STAMPED_REFERENCE, BIG_DECIMAL, BIG_INTEGER, BOOLEAN, BOOLEAN_2D_ARRAY, BOOLEAN_ARRAY, BYTE, BYTE_2D_ARRAY, BYTE_ARRAY, CHAR_2D_ARRAY, CHAR_ARRAY, CHAR_SEQUENCE, CHARACTER, CLASS, COLLECTION, COMPLETABLE_FUTURE, COMPLETION_STAGE, DATE, DOUBLE, DOUBLE_2D_ARRAY, DOUBLE_ARRAY, DOUBLE_PREDICATE, DOUBLE_STREAM, DURATION, FILE, FLOAT, FLOAT_2D_ARRAY, FLOAT_ARRAY, FUTURE, INPUT_STREAM, INSTANT, INT_2D_ARRAY, INT_ARRAY, INT_PREDICATE, INT_STREAM, INTEGER, ITERABLE, ITERATOR, LIST, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_2D_ARRAY, LONG_ADDER, LONG_ARRAY, LONG_PREDICATE, LONG_STREAM, MAP, MATCHER, OFFSET_DATE_TIME, OFFSET_TIME, OPTIONAL, OPTIONAL_DOUBLE, OPTIONAL_INT, OPTIONAL_LONG, PATH, PERIOD, PREDICATE, SET, SHORT, SHORT_2D_ARRAY, SHORT_ARRAY, SPLITERATOR, STREAM, STRING, STRING_BUFFER, STRING_BUILDER, TEMPORAL, THROWABLE, URI_TYPE, URL_TYPE, YEAR_MONTH, ZONED_DATE_TIME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic InputGuardrailResultAssertassertThat(InputGuardrailResult actual) Starts assertions on the given input guardrail result.static OutputGuardrailResultAssertassertThat(OutputGuardrailResult actual) Starts assertions on the given output guardrail result.static ChatRequestAssertassertThat(ChatRequest actual) Starts assertions on the given request sent to a chat model, for examplecapture.firstRequest().static AiServiceInvocationAssertassertThat(AiServiceCapture actual) Starts assertions on the only top-level AI Service invocation of the given capture, made withAiServiceCapture.static AiServiceInvocationAssertassertThat(AiServiceInvocation actual) Starts assertions on the given AI Service invocation, captured withAiServiceCapture.Methods inherited from class org.assertj.core.api.Assertions
allOf, allOf, allOf, anyOf, anyOf, anyOf, as, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThatCharSequence, assertThatCode, assertThatCollection, assertThatComparable, assertThatException, assertThatExceptionOfType, assertThatIllegalArgumentException, assertThatIllegalStateException, assertThatIndexOutOfBoundsException, assertThatIOException, assertThatIterable, assertThatIterator, assertThatList, assertThatNoException, assertThatNullPointerException, assertThatObject, assertThatPath, assertThatPredicate, assertThatReflectiveOperationException, assertThatRuntimeException, assertThatStream, assertThatTemporal, assertThatThrownBy, assertThatThrownBy, assertWith, atIndex, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, catchException, catchIllegalArgumentException, catchIllegalStateException, catchIndexOutOfBoundsException, catchIOException, catchNullPointerException, catchReflectiveOperationException, catchRuntimeException, catchThrowable, catchThrowableOfType, catchThrowableOfType, contentOf, contentOf, contentOf, contentOf, contentOf, contentOf, doesNotHave, entry, extractProperty, extractProperty, fail, fail, fail, fail, fail, failBecauseExceptionWasNotThrown, filter, filter, from, in, linesOf, linesOf, linesOf, linesOf, linesOf, linesOf, linesOf, linesOf, linesOf, not, not, notIn, offset, offset, registerCustomDateFormat, registerCustomDateFormat, registerFormatterForType, setAllowComparingPrivateFields, setAllowExtractingPrivateFields, setDescriptionConsumer, setExtractBareNamePropertyMethods, setLenientDateParsing, setMaxElementsForPrinting, setMaxLengthForSingleLineDescription, setMaxStackTraceElementsDisplayed, setPrintAssertionsDescription, setRemoveAssertJRelatedElementsFromStackTrace, shouldHaveThrown, tuple, useDefaultDateFormatsOnly, useDefaultRepresentation, useRepresentation, within, within, within, within, within, within, within, within, within, within, withinPercentage, withinPercentage, withinPercentage, withMarginOf, withPrecision, withPrecision
-
Constructor Details
-
LangChain4jAssertions
protected LangChain4jAssertions()
-
-
Method Details
-
assertThat
Starts assertions on the given AI Service invocation, captured withAiServiceCapture. -
assertThat
Starts assertions on the only top-level AI Service invocation of the given capture, made withAiServiceCapture.- Throws:
AssertionError- if the capture has no top-level invocation or more than one: useassertThat(capture.invocations().get(i))then
-
assertThat
Starts assertions on the given request sent to a chat model, for examplecapture.firstRequest(). -
assertThat
Starts assertions on the given input guardrail result. -
assertThat
Starts assertions on the given output guardrail result.
-