- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我模拟了一个名为 methodA()
的方法。
我有一个名为 linkedListA
的链表。现在,
我有一行代码来模拟 methodA
的返回,这样
when(methodA()).thenReturn(linkedListA.get(0)).thenReturn(linkedListA.get(1)).thenReturn(linkedListA.get(2))
等等
现在,是否有一种更有效/更清晰的方式来编写所有的 thenReturns
,例如,就像在一个循环中一样?所以,我不必写大量的 thenReturns
谢谢
最佳答案
我认为第一个改进是使用这个:
when(methodA()).thenReturn(linkedListA.get(0), linkedListA.get(1), linkedListA.get(2)) and so on
此外,您可以使用 thenAnswer
方法返回一个值:
final AtomicInteger i = new AtomicInteger(0);
when(methodA()).thenAnswer(new Answer<YourType>() {
@Override
public YourType answer(InvocationOnMock invocation) {
return linkedListA.get(i.getAndIncrement());
}
});
例子:
import static org.powermock.api.mockito.PowerMockito.when;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Blub.class)
public class Mockexample {
@Test
public void test() {
Blub blub = PowerMockito.mock(Blub.class);
final List<Integer> counter = Arrays.asList(1, 2, 3);
final AtomicInteger i = new AtomicInteger(0);
// blub.size() is final, only reason to use PowerMockito here
when(blub.size()).thenAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return counter.get(i.getAndIncrement());
}
});
System.out.println(blub.size());
System.out.println(blub.size());
System.out.println(blub.size());
}
}
Blub 类:
public class Blub {
public final int size() {
return 0;
}
}
关于java - Powermockito : using thenReturn efficiently,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11458409/
public class MainClass { public void method1() { ……. String str = getMethod2();
我试图从when().theReturn 返回一个迭代器,但我不断收到此错误: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Itr
我正在尝试为我的 Spring Controller 编写测试,但遇到了问题。以下代码始终返回 redirect:/welcome 尽管我有 when(result.hasErrors()).then
这确实是一个新手问题,但我不知道如何解决这个问题。 我必须模拟一个方法来返回这样的类。 public Class getAClass(); 如果我做这样的事情 when(this.someInstan
经过大量研究,我没有在 Java 中的 JUnits 中找到这个问题的答案。 我想要做的是:对when().thenReturn(object)调用返回的对象调用一些方法。 例如: public b
我在测试中使用 PowerMockito 模拟静态缓存。一般来说,缓存的工作原理是这样的: Cache.getInstance().findEntityById(AbstractDTO); // so
我能够调整我的测试,但它失败了。问题是,模拟方法仍然返回错误数据。这是我要测试的方法: fun getTextByLanguage(list: List) : String { val dev
我有一个接受项目列表的方法。我希望模拟方法返回相同大小的相应列表(即 List ) 更一般地说,是否可以根据给定的输入动态设置返回值? 问题是我对同一个方法进行了多次测试。测试 A 用于空列表,测试
我已经阅读了此处的所有其他主题,但找不到正确的解决方案。 我正在测试一个调用我想模拟的服务的 Controller 。如果在测试中我控制了 when().thenReturn() 规则的结果,那么它就
我是 Mockito 的新手,我对 thenReturn 方法有疑问。我已经阅读了此类解决方案运行良好的教程,但在我的程序中,与上述示例相比,肯定存在任何不一致之处。 @RunWith(Mockito
我有一个具有 2 个函数的 A 类:函数 a() 返回一个随机数。调用 a() 并返回返回值的函数 b()。 在测试中我写了这个: A test = Mockito.mock(A.class) Moc
我有一个 Tuple 模拟类,它的 getString(0) 和 getString(1) 方法预计会被调用 n 次。而不是写类似的东西, when(tuple.getString(0)).thenR
我正在尝试实现 Mockito 来测试一个特定的方法,但是 .thenReturn(...) 似乎总是返回一个空对象而不是我想要的: 剪切: public class TestClassFacade
我正在研究继承的代码。我编写了一个应该捕获 NullPointerException 的测试(因为它试图从 null 对象调用方法) @Test(expected=NullPointerExcepti
我在 Mockito 中有这个: when(mockedMergeContext.createNewEntityOfType(IService.class)).thenReturn(new Servi
考虑以下类(使用 CDI + 剪切 Restclient) public class A { @Inject Restclient client; public Object init(Stri
我模拟了一个名为 methodA() 的方法。 我有一个名为 linkedListA 的链表。现在, 我有一行代码来模拟 methodA 的返回,这样 when(methodA()).thenRetu
我一直在试图弄清楚为什么当我有 when(controller.findIngredientsByCategory(any()).thenReturn(Collections.emptyList())
运行以下代码时,我收到错误消息 Unfinished Stubbing here detected: import static org.mockito.Mockito.mock; import st
测试类 public class CollectionImplementationUnitTest { CollectionImplementation colImp; public void
我是一名优秀的程序员,十分优秀!