Class Expect

java.lang.Object
io.karatelabs.match.Expect
All Implemented Interfaces:
io.karatelabs.js.ObjectLike, io.karatelabs.js.SimpleObject

public class Expect extends Object implements io.karatelabs.js.SimpleObject
Chai-style BDD assertion API for Karate.

Usage:

 karate.expect(actual).to.equal(expected)
 karate.expect(actual).to.be.a('string')
 karate.expect(actual).to.have.property('name')
 karate.expect(actual).to.not.equal(unexpected)
 
  • Constructor Details

    • Expect

      public Expect(Object subject)
    • Expect

      public Expect(Object subject, BiConsumer<io.karatelabs.js.Context,Result> onResult)
    • Expect

      public Expect(Object subject, boolean negated, BiConsumer<io.karatelabs.js.Context,Result> onResult, boolean throwOnFailure)
      Creates an Expect instance with control over throw behavior.
      Parameters:
      subject - the value to make assertions on
      negated - whether assertions should be negated
      onResult - callback for handling assertion results (can be null)
      throwOnFailure - if true, throws AssertionError on failures; if false, only calls onResult
    • Expect

      public Expect(Object subject, boolean negated, BiConsumer<io.karatelabs.js.Context,Result> onResult, boolean throwOnFailure, Supplier<io.karatelabs.js.Context> contextSupplier)
      Creates an Expect instance with full control over behavior.
      Parameters:
      subject - the value to make assertions on
      negated - whether assertions should be negated
      onResult - callback for handling assertion results (can be null)
      throwOnFailure - if true, throws AssertionError on failures; if false, only calls onResult
      contextSupplier - optional supplier for getting a context when none is available (e.g., for property-style assertions)
  • Method Details

    • getSourceFromContext

      public static String getSourceFromContext(io.karatelabs.js.Context context)
      Extracts the source expression from the JS Context AST. Used for including the original expression in error messages.
    • jsGet

      public Object jsGet(String key)
      Specified by:
      jsGet in interface io.karatelabs.js.SimpleObject
    • getSubject

      public Object getSubject()
    • isNegated

      public boolean isNegated()
    • getOnResult

      public BiConsumer<io.karatelabs.js.Context,Result> getOnResult()
    • isThrowOnFailure

      public boolean isThrowOnFailure()
    • getContextSupplier

      public Supplier<io.karatelabs.js.Context> getContextSupplier()