IAssert and IAssertLifecycle in TestNG
IAssert and IAssertLifecycle in TestNG
In TestNG, IAssert and IAssertLifecycle are interfaces used internally for handling assertions, especially with SoftAssert.
IAssert
IAssert is an interface that represents a single assertion operation. It contains the details required to perform an assertion such as the expected value, actual value, and assertion message.
Main Purpose: Represents an individual assertion check.
Example Structure
public interface IAssert<T> {
String getMessage();
void doAssert();
T getActual();
T getExpected();
}
Whenever a soft assertion like assertEquals() is used, TestNG internally creates an IAssert object to store the assertion information.
IAssertLifecycle
IAssertLifecycle is an interface that defines the lifecycle events of an assertion. It allows TestNG to manage what happens before and after an assertion is executed.
Main Purpose: Controls the execution flow of assertions.
Example Structure
public interface IAssertLifecycle {
void onBeforeAssert(IAssert<?> assertCommand);
void onAssertSuccess(IAssert<?> assertCommand);
void onAssertFailure(IAssert<?> assertCommand, AssertionError ex);
void onAfterAssert(IAssert<?> assertCommand);
}
These methods are used internally by SoftAssert to track assertion execution. Relationship between IAssert and IAssertLifecycle
- IAssert represents the assertion data and logic.
- IAssertLifecycle manages how the assertion is executed.
- SoftAssert uses both interfaces internally.
IAssert vs IAssertLifecycle
| Feature | IAssert | IAssertLifecycle |
|---|---|---|
| Type | Interface | Interface |
| Purpose | Represents a single assertion | Manages assertion lifecycle |
| Focus | Assertion logic and data | Assertion execution events |
| Used By | SoftAssert | SoftAssert |
| Main Role | Stores assertion information | Controls assertion flow |
IAssert represents a single assertion and contains the assertion logic.
IAssertLifecycle defines lifecycle methods that control how assertions are executed.