无论我做什么,尝试模拟方法时都会收到以下错误
java.lang.IllegalStateException:前面的方法调用缺少行为定义:ConfigurationSection.get(“国家/地区语言”)用法是:Expect(a.foo())。andXXX()
我的测试代码:
EasyMock.expect(section.getString("country-language")).andReturn("US");
LocaleManager.updateLocale(section, Collections.emptyList());
EasyMock.expectLastCall();
replayAll();
Assert.assertEquals("Test", TranslatedMessage.translate("test"));
verifyAll();
expect andReturn被模拟的类调用,而静态的upateLocale方法首先调用该方法。奇怪的是,此测试工作正常:
EasyMock.expect(section.getString("country-language")).andReturn("US");
replayAll();
Assert.assertEquals("US", section.getString("country-language"));
verifyAll();
但是从外部方法调用它不起作用。
您的模拟物说:
EasyMock.expect(section.getString("country-language"))
错误说:
ConfigurationSection.get("country-language")
您不是在嘲笑get("country-language")
。您正在嘲笑getString("country-language")
。
无关,但verify
是维护的噩梦,通常应避免。这将测试代码直接与实现联系在一起。如果可能,测试应集中在输入和输出上。