Class MocksApi
- All Implemented Interfaces:
MocksConfig,MocksInt
It is recommended to use a single instance:
MocksApi mockApi = MocksApi.getInstance();
Enchanted Report for mocks verify default:ON: enchantedReport, can be disabled by: setEnchantedReport(boolean))
Default await timeout for assertions with await is configured by awaitMs.
It can be changed using setAwaitMs(int) or configuration.
- Since:
- 1.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intDefault await polling interval in milliseconds for tests. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidassertAllMocksCount(int receivedCount) Asserts that the total number of requests to mock-server matches the expected count.voidassertAllMocksCount(int from, int to) Asserts that the total number of requests to mock-server is within the specified range.voidassertMocksCountWithAwait(int from, int to) Asserts that the total number of requests to mock-server is within the specified range.static StringbaseAuthGenerate(String user, String pass) voidClean mock-server logs (requests/responses)voidcreateMock(String mockExpectation) Creates a mock-server expectation.voidcreateMockCustom(String description, String path) Creates a mock-server expectation loaded from a file.Returns and logs (at INFO level) a human-readable summary of all resolved configuration values.static MocksApiReturns the instance ofMocksApiwith config builderMocksApi().getRequestValue(String mockSetup, String extractPath) Returns a value extracted from a mock-server request (body, header, or other source).voidReset mock-server.setAwaitMs(int awaitMs) Configures the global await timeout for assertions and operations that use await.setEnchantedReport(boolean enchantedReport) Enabling or disabling enchanted mockVerify report.setLogging(boolean enable) Enables or disables logging manually (debug log level will print logs anyway!).voidAdds an attachment with a list of mock-server requests (only HTTP method and path).voidAdds an attachment with mock-server logs (requests/responses).voidAdd attachment with requests to mock-servervoidverifyMock(String verifySetup) Verifies mock-server requests according to the provided verification setup.voidverifyMockCustom(String description, String path) Verifies mock-server requests according to the verification setup loaded from a file.voidverifyMockCustomWithAwait(String description, String path) Verifies mock-server requests according to the verification setup loaded from a file.voidverifyMockSequence(String verifySequenceSetup) Verifies mock-server requests according to the verification setup loaded from a file.voidverifyMockSequenceWithAwait(String verifySequenceSetup) Verifies mock-server requests according to the sequence verification setupvoidverifyMockWithAwait(String verifySetup) Verifies mock-server requests according to the provided verification setup.withAwaitMs(int specificAwaitMs) Configure await for next assert with await (than await rollback to global).
-
Field Details
-
awaitPollInterval
protected volatile int awaitPollIntervalDefault await polling interval in milliseconds for tests.
-
-
Constructor Details
-
MocksApi
This constructor initializes client for interaction with mock-server- Parameters:
url- mock-server url ("http://localhost")port- mock-server port
-
MocksApi
public MocksApi()Constructs a mock-server Api client using YAML configuration.Loads configuration values from a YAML file.
Default file:
bugreaper.ymlCustom file: using
-DbugreaperEnv=testloadsbugreaper-test.ymlmodules: mocks: url: http://localhost port: 1082 await: 440 # (optional) logging: true # (optional) enchanted-report: false # (optional)Missing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.
- Throws:
IllegalArgumentException- if the configuration contains invalid values
-
-
Method Details
-
getInstance
Returns the instance ofMocksApiwith config builderMocksApi().This implementation is thread-safe using method-level synchronization.
- Returns:
- the shared instance of
MocksApi - Throws:
IllegalArgumentException- if the configuration contains invalid values- See Also:
-
setAwaitMs
Description copied from interface:MocksConfigConfigures the global await timeout for assertions and operations that use await.- Specified by:
setAwaitMsin interfaceMocksConfig- Parameters:
awaitMs- await timeout in milliseconds- Returns:
- this
-
withAwaitMs
Description copied from interface:MocksConfigConfigure await for next assert with await (than await rollback to global).global
MocksConfig.setAwaitMs(int)will be ignored- Specified by:
withAwaitMsin interfaceMocksConfig- Parameters:
specificAwaitMs- ms await- Returns:
- this instance for method chaining
-
setLogging
Description copied from interface:MocksConfigEnables or disables logging manually (debug log level will print logs anyway!).- Specified by:
setLoggingin interfaceMocksConfig- Parameters:
enable- true=request/response logging (Allure on always!)- Returns:
- this instance for method chaining
-
setEnchantedReport
Description copied from interface:MocksConfigEnabling or disabling enchanted mockVerify report.Same behavior as
MockEnchantedDiffer.- Specified by:
setEnchantedReportin interfaceMocksConfig- Parameters:
enchantedReport- true=on/false=off- Returns:
- this instance for method chaining
-
getConfigSummary
Description copied from interface:MocksConfigReturns and logs (at INFO level) a human-readable summary of all resolved configuration values.The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.
- Specified by:
getConfigSummaryin interfaceMocksConfig- Returns:
- String with summary
-
resetMocks
@Step("(MOCK)[RESET]: reset mock-server") public void resetMocks()Description copied from interface:MocksIntReset mock-server.Clean all: logs, requests, expectations
- Specified by:
resetMocksin interfaceMocksInt
-
cleanMockLogs
@Step("(MOCK)[CLEAN] Clean all recorded log messages from mock-server") public void cleanMockLogs()Description copied from interface:MocksIntClean mock-server logs (requests/responses)- Specified by:
cleanMockLogsin interfaceMocksInt
-
showMockLogs
@Step("(MOCK)[LOGS] Retrieve all recorded log messages from mock-server") public void showMockLogs()Description copied from interface:MocksIntAdds an attachment with mock-server logs (requests/responses).At DEBUG level, logs are also printed to the console.
- Specified by:
showMockLogsin interfaceMocksInt
-
showMockRequests
@Step("(MOCK)[LOGS] Retrieve all requests to mock-server") public void showMockRequests()Description copied from interface:MocksIntAdd attachment with requests to mock-server- Specified by:
showMockRequestsin interfaceMocksInt
-
showBaseRequestsList
public void showBaseRequestsList()Description copied from interface:MocksIntAdds an attachment with a list of mock-server requests (only HTTP method and path).- Specified by:
showBaseRequestsListin interfaceMocksInt
-
createMock
@Step("(MOCK)[CREATE] Create mock-server expectation") public void createMock(@Param(mode=HIDDEN) String mockExpectation) Description copied from interface:MocksIntCreates a mock-server expectation.- Specified by:
createMockin interfaceMocksInt- Parameters:
mockExpectation- JSON string containing the mock-server expectation definition
-
createMockCustom
Description copied from interface:MocksIntCreates a mock-server expectation loaded from a file.- Specified by:
createMockCustomin interfaceMocksInt- Parameters:
description- description part for steppath- path to the resource file with mock-server expectation
-
assertAllMocksCount
@Step("(MOCK)[VERIFY] Mock-server received exactly <{receivedCount}> requests") public void assertAllMocksCount(int receivedCount) Description copied from interface:MocksIntAsserts that the total number of requests to mock-server matches the expected count.- Specified by:
assertAllMocksCountin interfaceMocksInt- Parameters:
receivedCount- expected exact number of requests
-
assertAllMocksCount
@Step("(MOCK)(VERIFY) Mock-server received requests from <{from}> to <{to}>") public void assertAllMocksCount(int from, int to) Description copied from interface:MocksIntAsserts that the total number of requests to mock-server is within the specified range.- Specified by:
assertAllMocksCountin interfaceMocksInt- Parameters:
from- minimum expected number of requeststo- maximum expected number of requests
-
verifyMockCustom
Description copied from interface:MocksIntVerifies mock-server requests according to the verification setup loaded from a file.- Specified by:
verifyMockCustomin interfaceMocksInt- Parameters:
description- description part for steppath- path to the resource file with mock-server verification setup
-
verifyMock
@Step("(MOCK)[VERIFY] Verify mock-server request") public void verifyMock(@Param(mode=HIDDEN) String verifySetup) Description copied from interface:MocksIntVerifies mock-server requests according to the provided verification setup.- Specified by:
verifyMockin interfaceMocksInt- Parameters:
verifySetup- JSON string containing the mock-server verification setup
-
verifyMockSequence
@Step("(MOCK)[VERIFY] Verify mock-server request sequence") public void verifyMockSequence(String verifySequenceSetup) Description copied from interface:MocksIntVerifies mock-server requests according to the verification setup loaded from a file.- Specified by:
verifyMockSequencein interfaceMocksInt- Parameters:
verifySequenceSetup- JSON string containing the mock-server sequence verification setup
-
verifyMockCustomWithAwait
Description copied from interface:MocksIntVerifies mock-server requests according to the verification setup loaded from a file.Uses await.
- Specified by:
verifyMockCustomWithAwaitin interfaceMocksInt- Parameters:
description- description part for steppath- path to the resource file with mock-server verification setup
-
verifyMockSequenceWithAwait
@Step("(MOCK)[VERIFY] Verify mock-server request sequence using await") public void verifyMockSequenceWithAwait(@Param(mode=HIDDEN) String verifySequenceSetup) Description copied from interface:MocksIntVerifies mock-server requests according to the sequence verification setupUses await.
- Specified by:
verifyMockSequenceWithAwaitin interfaceMocksInt- Parameters:
verifySequenceSetup- JSON string containing the mock-server sequence verification setup
-
verifyMockWithAwait
@Step("(MOCK)[VERIFY] Verify mock-server request using await") public void verifyMockWithAwait(@Param(mode=HIDDEN) String verifySetup) Description copied from interface:MocksIntVerifies mock-server requests according to the provided verification setup.Uses await.
- Specified by:
verifyMockWithAwaitin interfaceMocksInt- Parameters:
verifySetup- JSON string containing the mock-server verification setup
-
assertMocksCountWithAwait
@Step("(MOCK)(VERIFY) Mock-server received requests from <{from}> to <{to}> using await") public void assertMocksCountWithAwait(int from, int to) Description copied from interface:MocksIntAsserts that the total number of requests to mock-server is within the specified range.Uses await.
- Specified by:
assertMocksCountWithAwaitin interfaceMocksInt- Parameters:
from- minimum expected number of requeststo- maximum expected number of requests
-
getRequestValue
@Step("[MOCK]: Get value from request {extractPath}") public Object getRequestValue(String mockSetup, String extractPath) Description copied from interface:MocksIntReturns a value extracted from a mock-server request (body, header, or other source).Can be used to retrieve dynamic data (for example, ID, hash, or Base64 value) required for subsequent assertions.
var value = mocksApi.getRequestValue( """ { "method": "POST", "path": "/api/user" }""", "[0].body.json.name"); //grab value from key "name" from body of first request by condition- Specified by:
getRequestValuein interfaceMocksInt- Parameters:
mockSetup- - setup for requestextractPath- - json path in request to mock (example:"[0].body.json.id")- Returns:
- Object (any types)
-
baseAuthGenerate
-