gpt4 book ai didi

java - EasyMock 缺少行为,即使它已定义

转载 作者:行者123 更新时间:2023-12-01 18:07:42 45 4
gpt4 key购买 nike

无论我做什么,在尝试模拟方法时都会收到以下错误

java.lang.IllegalStateException: missing behavior definition for the preceding method call: ConfigurationSection.get("country-language") Usage is: 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 和Return,并且静态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")

你不是在 mock get("country-language")。您正在 mock getString("country-language")

不相关,但验证是维护的噩梦,通常应该避免。这将测试代码直接与实现联系起来。如果可能的话,测试应该关注输入和输出。

关于java - EasyMock 缺少行为,即使它已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60531100/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com