Mock Command and Query Gateways

Hi Team,

I’m little new for Axon, need an help in writing test cases on Command and Query Gateways. Can you suggest how we can mock Command/Query Gateway and its method results.

Ex:- I want to test below code snippet.

public SomeType someMethod(){
CompletableFuture future= queryGateway.query(SomeQuery);

Now I want to mock queryGateway’s query method and its results, as part of method test.


Since you are talking about mocking - I am assuming you are referring to unit tests rather than integration tests.

This is not tested code, but try something along the lines of the following in the class you are testing:

import static org.mockito.Mockito.*;

// Mock as an field in the test class

private QueryGateway queryGatewayMock;

void setUpBeforeEachTest() {
// Reset the mock before each test.

queryGatewayMock = mock(QueryGateway.class)


void someMethod_shouldDoSomething() {
// arrange the mock (here I’m mocking some ‘OrderState’)
var orderState = …

when( queryGatewayMock .query(any(OrderState.class), …)).thenReturn(orderStateToReturn); // obviously override the correct query method.

// act
SomeType st = class_under_test.someMethod();

// assert


Hope this helps!

Thank you, it helped me to go further step.