Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easymock returning null instead of my value

Tags:

easymock

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

like image 834
Dean Hiller Avatar asked Dec 30 '25 14:12

Dean Hiller


1 Answers

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.

like image 54
Dan Temple Avatar answered Jan 02 '26 02:01

Dan Temple