Class VCRExtension

java.lang.Object
com.redis.vl.test.vcr.VCRExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.TestWatcher

public class VCRExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.TestWatcher
JUnit 5 extension that provides VCR (Video Cassette Recorder) functionality for recording and playing back LLM API calls during tests.

This extension manages:

  • Redis container lifecycle with AOF/RDB persistence
  • Cassette storage and retrieval
  • Test context and call counter management
  • Automatic wrapping of @VCRModel annotated fields

Usage with declarative field wrapping:


 @VCRTest(mode = VCRMode.PLAYBACK_OR_RECORD)
 class MyLLMTest {

     @VCRModel
     private EmbeddingModel embeddingModel;

     @VCRModel
     private ChatModel chatModel;

     @BeforeEach
     void setup() {
         // Initialize models normally - VCR wraps them automatically
         embeddingModel = new OpenAiEmbeddingModel(...);
         chatModel = new OpenAiChatModel(...);
     }

     @Test
     void testLLMCall() {
         // LLM calls are automatically recorded/replayed
         embeddingModel.embed("Hello");
         chatModel.generate("What is Redis?");
     }
 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    testFailed(org.junit.jupiter.api.extension.ExtensionContext extensionContext, Throwable cause)
     
    void
    testSuccessful(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.extension.TestWatcher

    testAborted, testDisabled
  • Constructor Details

    • VCRExtension

      public VCRExtension()
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
      Throws:
      Exception
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • testSuccessful

      public void testSuccessful(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      testSuccessful in interface org.junit.jupiter.api.extension.TestWatcher
    • testFailed

      public void testFailed(org.junit.jupiter.api.extension.ExtensionContext extensionContext, Throwable cause)
      Specified by:
      testFailed in interface org.junit.jupiter.api.extension.TestWatcher
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      Throws:
      Exception