Mockserver expectation not active

Viewed 594

Context is a Junit test, setup with Spring-boot, Mockserver and Camel.

Problem is that mocked response of the verified request is not matched during the test.

The logs :

INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 received request:

  {
    "method" : "GET",
    "path" : "/api/installation/organizations",
    "headers" : {
      "content-length" : [ "0" ],
      "User-Agent" : [ "Java/11.0.13" ],
      "Host" : [ "localhost:8087" ],
      "Connection" : [ "keep-alive" ],
      "Accept" : [ "application/json" ]
    },
    "keepAlive" : true,
    "secure" : false
  }

INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 no expectation for:

  {
    "method" : "GET",
    "path" : "/api/installation/organizations",
    "headers" : {
      "content-length" : [ "0" ],
      "User-Agent" : [ "Java/11.0.13" ],
      "Host" : [ "localhost:8087" ],
      "Connection" : [ "keep-alive" ],
      "Accept" : [ "application/json" ]
    },
    "keepAlive" : true,
    "secure" : false
  }

 returning response:

  {
    "statusCode" : 404,
    "reasonPhrase" : "Not Found"
  }

INFO 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : Started CsvRouteBuilderTest in 6.17 seconds (JVM running for 8.32)
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : retrieved requests and responses in json that match:

  { }

DEBUG 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : RecordedRequestsAndResponses: 12
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 retrieved 0 active expectations in json that match:

  { }

DEBUG 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : activeExpectations: 0
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : retrieved recorded expectations in json that match:

  { }

DEBUG 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : recordedExpectations: 0
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 verifying requests that match:

  {
    "httpRequest" : {
      "method" : "GET",
      "path" : "/api/installation/organizations"
    },
    "times" : {
      "atLeast" : 1
    }
  }

INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : request:

  {
    "method" : "GET",
    "path" : "/api/installation/organizations"
  }

 found at least once

And the code :

@ExtendWith(MockServerExtension.class)
@MockServerSettings(ports = {8087})
@SpringBootTest
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@DisableJmx
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class CsvRouteBuilderTest {

    @Autowired
    ProducerTemplate producerTemplate;

    private MockServerClient mockServerClient;

    @BeforeEach
    public void beforeEachLifecyleMethod(MockServerClient client) {
        mockServerClient = client;
    }
    
    @Before
    public void setupMocks() {
        mockServerClient
            .when(
                request().withMethod("GET").withPath(HDConnectProxyRestTemplate.URI_GET_INSTALLATION_ORGANIZATIONS)
                    .withHeader(Header.header("Accept", MediaType.APPLICATION_JSON.toString())))
            .respond(
                response("{data}").withContentType(MediaType.APPLICATION_JSON));
    }

What did I miss ? I can see that both Camel and Mockserver are running at the moment of the test. But the mocked response should be triggered.

2 Answers

I found out that spring-boot's context is starting the Camel logic before the unit test itself.

So, at the test, the Camel route was already running, and the mock-server expectations were not available.

In order to properly test this situation, I needed to :

  1. First prevent the Camel route to start (for example with the Route.configure() method),
  2. Initialize the mocks and their expectations (@Before each test),
  3. Then, specifically enable the Camel route, during the Junit test.

As the MockServerExtension starts the mock server thanks to the BeforeAllCallback, another way could be to initialize the mocker server beforeAll as next:

@BeforeAll
static void setupMocks(MockServerClient mockServerClient) {
    mockServerClient
        .when(
            request().withMethod("GET").withPath(HDConnectProxyRestTemplate.URI_GET_INSTALLATION_ORGANIZATIONS)
                .withHeader(Header.header("Accept", MediaType.APPLICATION_JSON.toString())))
        .respond(
            response("{data}").withContentType(MediaType.APPLICATION_JSON));
}

This way your mock server will be configured before the start up of Camel.

Related