Question

I am not sure why "metric" ends up being null. I want the method to always return the same value for my test

    CountMetric countMetric = new CountMetricStub("count");
    expect(metricManager.getOrCreateCountMetric(anyObject(String.class))).andStubReturn(countMetric);

    CountMetric metric = metricManager.getOrCreateCountMetric("ASDF");

    assertNotNull(metric);

any ideas what I am doing wrong here?

thanks, Dean

Was it helpful?

Solution

You need to replay the mock

CountMetric countMetric = new CountMetricStub("count");
expect(metricManager.getOrCreateCountMetric(anyObject(String.class))).andStubReturn(countMetric);
EasyMock.replay(metricManager); //Add this line

CountMetric metric = metricManager.getOrCreateCountMetric("ASDF");

assertNotNull(metric);

Currently, the metricManager is still in record mode which means any calls to its methods just perform default behaviour.

If you have a call to EasyMock.verify() in there too (without the call to replay()) EasyMock tells you that you can't call verify while in record mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top