Java EasyMock缺少行为,即使它';定义
无论我做什么,在尝试模拟方法时都会出现以下错误 java.lang.IllegalStateException:前面的方法调用缺少行为定义: ConfigurationSection.get(“国家语言”) 用法是:expect(a.foo()).andXXX() 我的测试代码:Java EasyMock缺少行为,即使它';定义,java,junit,easymock,Java,Junit,Easymock,无论我做什么,在尝试模拟方法时都会出现以下错误 java.lang.IllegalStateException:前面的方法调用缺少行为定义: ConfigurationSection.get(“国家语言”) 用法是:expect(a.foo()).andXXX() 我的测试代码: EasyMock.expect(section.getString("country-language")).andReturn("US"); LocaleManager.updateLocale(section,
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(“国家语言”)
。你在模仿getString(“国家语言”)
不相关,但
verify
是维护的噩梦,通常应避免。这将测试代码直接绑定到实现。如果可能的话,测试应该关注输入和输出。我忘记了自己的实现,它一直在我的眼皮底下!谢谢你的帮助,我花了好几个小时试图让任何东西都能正常工作。我刚刚意识到我之前使用PowerMock时使用了相同的方法,我想我的代码默认值是正确的。必须将测试修复为非默认语言。