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)
 
  • Field Summary

    Fields inherited from interface io.karatelabs.js.SimpleObject

    logger, TO_STRING
  • Constructor Summary

    Constructors
    Constructor
    Description
    Expect(Object subject)
     
    Expect(Object subject, boolean negated, BiConsumer<io.karatelabs.js.Context,Result> onResult, boolean throwOnFailure)
    Creates an Expect instance with control over throw behavior.
    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.
    Expect(Object subject, BiConsumer<io.karatelabs.js.Context,Result> onResult)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Supplier<io.karatelabs.js.Context>
     
    BiConsumer<io.karatelabs.js.Context,Result>
     
    static String
    getSourceFromContext(io.karatelabs.js.Context context)
    Extracts the source expression from the JS Context AST.
     
    boolean
     
    boolean
     
     

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface io.karatelabs.js.ObjectLike

    getMember, getPrototype, isExtensible, isFrozen, isOwnProperty, isSealed, putMember, removeMember, setExtensible, setFrozen, setSealed

    Methods inherited from interface io.karatelabs.js.SimpleObject

    getMember, jsKeys, jsToString, putMember, removeMember, toMap
  • 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()