- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用mockito为相同的函数调用返回不同的值:
doAnswer(new Answer() {
int counter = 0;
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (counter == 0) {
counter += 1;
return object1;
} else {
return object2;
}
}
}).when(thing).thingFunction();
thingFunction
目前仅被调用一次,但是,在第一次调用时,mockito 开始一遍又一遍地自调用(3-5 次),从而增加该计数器。不知道为什么会发生这种情况。这是正确的吗?
最佳答案
除了语句中存在警告之外,您的代码应该是正确的,因为 Answer 是一个泛型类。你应该写new Answer<Object>() { //.. }
(根据您的模拟方法的返回类型)
为了澄清,我用 Mockito 1.10.19 编写了一个 Junit 测试:
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestClass {
Object object1 = new Object();
Object object2 = new Object();
class Thing{
public Object thingFunction(){
return null;
}
}
@Test
public void test(){
Thing thing = Mockito.mock(Thing.class);
Mockito.doAnswer(new Answer<Object>() {
int counter = 0;
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (counter == 0) {
counter += 1;
return object1;
} else {
return object2;
}
}
}).when(thing).thingFunction();
assertEquals(object1, thing.thingFunction());
assertEquals(object2, thing.thingFunction());
}
}
关于java - Mockito自调用doAnswer重复调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51273197/
当抛出异常时,我可以以某种方式使用doAnswer()吗? 我在集成测试中使用它来获取方法调用并配置@RabbitListenerTest... @RunWith(SpringRunner.class
我试图在仅从特定类调用时模拟方法,因为底层框架调用我的模拟方法 N 次。 是否有一种方法可以描述特定模拟方法的调用者,以便我可以根据调用者有条件地返回数据? 我正在使用 Mockito 和 doAns
我正在尝试从具有 2 个重载的 gradle 类中 stub 一个 void 方法,并且 mockito 匹配器与 wearg 方法匹配,而我的 stub 失败了。这是我到目前为止所拥有的: doAn
我有一个类,本质上是这样的: class Checkpointer { public Output runFunction(Input input, Function function) {
这不会编译。我收到一个 UnfinishedStubbingException。我已经阅读了 Mockito api 和本网站上的其他问题,我认为我的语法应该没有错误,但它在 doAnswer(new
我正在使用的静态方法之一,它做了两件事。它返回一些数据,但它也修改传递给它的参数对象。这个更新后的参数对象随后会在代码中使用。 我正在使用 PowerMock 模拟返回行为。 为了定义第二部分 - 更
我正在尝试测试一个静态方法,该方法捕获来自不同子调用或方法本身的异常,并且只记录异常消息。根据要求,我无法更改方法实现。在这种情况下,监视子调用不是一种选择,因为我们希望在测试中保持一致,并且由于某些
这是我第一天使用 Mockito 编写单元测试,我可能从一个复杂的练习开始。 下面是我的类结构,我正在为 Class2.targetMethod() 编写测试。 Class1 静态方法修改传入的对象而
今天我学习了 Mockito,在玩弄它的过程中我发现了一些我不明白的地方。 假设我想测试以下代码: public void stop(boolean showMessage) { if
我想模拟我的 PermissionHostCompat 类的操作 requestPermission(@NonNull String Permission, int requestCode) 。 pu
用例: 我想用对象列表调用模拟类的方法,并想修改传递的参数。如果我处理某些具有返回类型的方法并且不适用于 Void 返回方法,它会工作正常。 测试方法: public class TestClass
我有一种情况,我的方法返回一些对象,方法包含一些参数,我有条件基于返回的响应和参数之一。 Map> testMap = new HashMap<>(); Object obj = new Object
我正在使用 Mockito 进行后期单元测试。我对何时使用 doAnswer 和 thenReturn 感到困惑。 谁能帮我详细介绍一下?到目前为止,我已经用 thenReturn 进行了尝试。 最佳
我在这段代码中使用 mockito spy: Mockito.doAnswer(new Answer() { Object answer(InvocationOnMock invocation
我想使用 when(spy).thenAnswer(new Answer())但似乎 thenAnswer 只适用于模拟而不适用于 spy 。我想使用 spy ,因为我只想 stub 特定方法。 有没
与这段 Java 代码等效的 Kotlin 是什么? doAnswer(new Answer() { @Override public Object answer(Invocation
当使用模拟进行单元测试时,我经常遇到需要检查模拟的某个方法是否使用正确的参数调用的情况。这意味着我必须以某种方式找到一种方法来查看传递给相关方法的内容。在 Spock 中,可以使用以下方法来完成: 1
我需要测试一种创建 Car 对象并使用 CarDao 将其保存在数据库中的方法。另一种方法创建 Car 对象,但如果 CarDao 是模拟的,则 car.getUser() 返回 null 而不是正确
我正在学习 mockito,我从 link 中了解了上述函数的基本用法。 . 但是我想知道它是否可以用于任何其他情况? 最佳答案 doThrow :主要用于在模拟对象中调用方法时抛出异常。 publi
我是一名优秀的程序员,十分优秀!