문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top