Hermes Mock

A thin library designed to mocking Hermes Frontend in tests.

Overview

Mocking is provided by HermesMock class, which consists of 3 parts: - HermesMockDefine - HermesMockExpect - HermesMockQuery

HermesMockDefine

Is responsible for defining new topics on Hermes, provides the following methods:

Response allows to define the following elements: - statusCode - a HTTP response code - fixedDelay - a response will be returned after the given time

HermesMockExpect

Is responsible for expectation of message on Hermes side, provides the following methods:

HermesMockQuery

Is responsible for querying Hermes for a received messages, provides the following methods:

Creating

To start using Hermes mock, add it as a dependency:

testImplementation group: 'pl.allegro.tech.hermes', name: 'hermes-mock', version: versions.hermes

Example

class MyServiceTest {

    @Rule
    HermesMockRule hermesMock = new HermesMockRule(8090);

    @Test
    public void exampleTest() {
        // given
        MyMessage myMessage = new MyMessage("id123", "content");
        String topicName = "myTopic";

        hermesMock.define().jsonTopic(topicName);

        // when
        myService.publishHermesMessage(myMessage);

        // then
        hermesMock.expect().singleJsonMessageOnTopicAs(topicName, MyMessage.class);

        // and
        List<MyMessage> all = hermesMock.query().allJsonMessagesAs(topicName, MyMessage.class);

        // and verify that `all` contains what we're expecting 
    }
}

JUnit5 / Spock2 automatic startup issues

If you're using JUnit5 or Spock2 HermesMockRule won't start automatically in your tests. In order to make it work you can:

class Junit5Test {

    private HermesMock hermesMock = new HermesMock.Builder().withPort(8090).build();

    @BeforeAll
    static void setup() {
        hermesMock.start();
    }

    @AfterAll
    static void cleanup() {
        hermesMock.stop();
    }

    @Test
    public void exampleTest() {
        // you can now use Hermes mock as in previous example
    }
}
testImplementation group: 'org.spockframework', name: 'spock-junit4', version: versions.spock