Unit Testing Erma Instrumentation

I love testing! Can I test my ERMA instrumentation?

Yes you can!!!

….Uh, mind telling me how?

Oh, sure. Basically, what you need to do is first add orbitz-api-monitoring-test to your test compile classpath. Then configure ERMA to use the MockMonitorProcessorFactory and MockMonitorProcessor. These classes will catch all Monitors created. When the Monitors are caught, you can assert that they contain the expected data.

Blah, blah, blah, mind just giving me the code?

Ok, I guess. Stick this in the setUp() method of your unit test. Make sure you retain a reference to that MockMonitorProcessor. You'll be querying it in your test cases to ensure you have the Monitors you expected.

_processor = new MockMonitorProcessor();
MockMonitorProcessorFactory processorFactory = new MockMonitorProcessorFactory(new MonitorProcessor[]{_processor});
 
MonitoringEngine mEngine = MonitoringEngine.getInstance();
mEngine.setProcessorFactory(processorFactory);
mEngine.setDecomposer(new MockDecomposer());
mEngine.restart();

When you're running your test case, the following methods will come in handy.
Monitor[] created = _processor.extractMonitorCreatedObjects();
Monitor[] started = _processor.extractMonitorStartedObjects();
Monitor[] processed = _processor.extractProcessObjects();

These methods will remove all the Monitor objects that were passed to the corresponding methods of the processor. You can then assert the expected attributes of your monitor using these instances.

Examples

There are two common mistakes that are made using TransactionMonitors. One is forgetting to call done() in a finally block, and the other is forgetting to call succeeded() in a try block. This test makes assertions for both:

public void testGetHttpResponseProperUrl() {
    String properUrl = "http://webservices.insurancebookers.com/" +
            "GetRates.asmx/Rates?&affiliateId=UK&fromDate=10-10-2006" +
            "&toDate=10-11-2006&adults=1&infants=0&child=0" +
            "&outboundLeg=GB-FR&outboundLegTime=00-2&inboundLeg=FR-GB" +
            "&inboundLegTime=00-2";
 
    HttpRequestSubmitter httpRequestSubmitter = new HttpRequestSubmitter();
    String httpResponse = httpRequestSubmitter.getHttpResponse(properUrl);
 
    assertNotNull(httpResponse);
 
    Monitor[] monitors = mockMonitorProcessor.extractProcessObjects();
    assertTrue("No Monitors processed, forgot to call TransactionMonitor.done() in finally block?", monitors.length>0);
    assertFalse("Monitor should not be marked as failed, forgot to call TransactionMonitor.succeeded() in try block?",
            monitors[0].getAsBoolean("failed"));
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License