Skip to content

Integration testing

Daria edited this page Oct 30, 2015 · 4 revisions

For integration testing MockMetricsService implementation of MetricsService interface should be used. Its located in separate package for easy test compile include. To add it to project classpath, use:

testCompile "org.ametiste.metrics:metrics-service-mock:${metricsVersion}"

Among with interface methods, MockMetricsService provides verify(..) and resetData() methods specifically for testing.

verify() accepts name of metric that is about to be verified as parameter, and creates MockMetricVerifier object. On this testing step no assertions are performed and no errors are expected. After verify() was once called, no MetricsService interface methods calls are allowed (i.e no metrics are gathered) and only verify() methods could be called before resetData() is invoked. This behaviour tends to guarantee metrics gathered are only for one exact integration run, and herefore could be checked for number and for existance in definite circumstances. Its required to use resetData() invoke in @Before or @After test methods.

#####Usage snippet:

@Autowired
private MockMetricsService metricsService;

@Before
public void setup() throws IOException, URISyntaxException {
    this.mockMvc = webAppContextSetup(this.wac).build();
}

@After
public void tearDown() throws Exception {
    metricsService.resetData();
}

@Test
public void testMetrics() throws Exception {
    MockRestServiceServer mockService = MockRestServiceServer.createServer(template);
    mockService
            .expect(org.springframework.test.web.client.match.MockRestRequestMatchers.method(HttpMethod.GET))
            .andRespond(withSuccess().contentType(MediaType.APPLICATION_JSON).body(reply));

    this.mockMvc
            .perform( get("/query?q=white cat").accept(MediaType.APPLICATION_JSON)
                            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));

    metricsService.verify("some.method.timing").registered().event().event(1);
    metricsService.verify("some.other.metric.timing").registered().event().event(1);
    metricsService.verify("error.counts").registered().increment().increment(1);
    metricsService.verify("results.number.gauge").registered().gauge().gauge(1).gauge(Arrays.asList(15));
    metricsService.verify("absent.metrics").notRegistered();

}

MockMetricsVerifier has several methods accessible to be called in chain, and are to be described in details:

  • registered() - assert that metric with name indicated in verify() is registered by service. No assertion of metric type happens in this call, only fact it was somehow registered.

  • notRegistered() - on opposite, asserts that metric with name indicated in verify() not registered by service. It guarantees no metric of ANY type is registered.

  • increment() - asserts that metric is registered by service as increment value (Countable or ErrorCountable mostly). No number of calls or values are checked.

  • increment(int times) - asserts that metric was registered by service as increment value times times, no values are checked.

  • increment(List values) - asserts that metric was registered by service as increment value with given values.

  • gauge() - asserts that metric is registered by service as gauge measure value( Gaugeable annotation)

  • gauge(int times) - asserts that metric is registered by service as gauge measure value times times

  • gauge(List values) - asserts that metric was registered by service as gauge measure value with given values.

  • event() - asserts that metric is registered by service as time or chrone measurement (Timeable, Chronable)

  • event(int times) - asserts that metric is registered by service as time or chrone measurement times times

  • event(List values) - asserts that metric is registered by service as time or chrone measurement with given values. Almost impossible to apply to assert time values, however is useful for chronable values assertion.

Verify methods could be called in chain. i.e. its possible to make step-by-step check by verify("myMetric").registered().event().event(1).event(Arrays.asList(12)); That is recommended way to do, since it provides more clear trace in case of assertion errors. However same result is guaranteed(though less clear and obvious) by verify("myMetric").event(Arrays.asList(12));

There are 4 methods that considered as 'endpoint' ones:

  • notRegistered()
  • increment(List values)
  • gauge(List values)
  • event(List values)

No futher calls could be done after it. In case of other calls no order is defined, however its obsolete to call registered after concrete metric type verify, or just type verify after times call verify. (i.e call verify("myMetric").increment().registered(); is obsolete, so as verify("myMetric").increment(1).increment();

To avoid test mess there's advice not to check different types of metrics in one chain even if they are named equally.