Tag: mockito

如何用mockito模拟一个构建器

我有一个建设者: class Builder{ private String name; private String address; public Builder setName(String name){ this.name = name; return this; } public Builder setAddress(String address){ this.address = address; return this; } } 在mockito中模拟构建器会使每个方法都为null。 因此,有一种简单的方法可以让构建器在每次函数调用时返回自己,而不使用when().thenReturn每个函数本身。

嘲弄保护方法

我想模拟一个inheritance的受保护方法。 我无法直接从java代码调用此方法,因为它inheritance自另一个包中的类。 我无法找到一种方法来指定这个方法来存入when(…) package a; public class A() { protected int m() {} } package b; public class B extends aA { // this class currently does not override m method from aA public asd() {} } // test package b; class BTest { @Test public void testClass() { B instance = PowerMockito.spy(new B()); PowerMockito.when(instance, […]

Enum值的unit testing不存在?

一些示例代码首先…… 枚举: public enum TestEnum { YES, NO } 一些代码: public static boolean WorkTheEnum(TestEnum theEnum) { switch (theEnum) { case YES: return true; case NO: return false; default: // throws an exception here } } 问题: TestEnum是我从不同开发人员的不同代码导入的东西。 所以它实际上可以改变。 对于这种情况,我想要进行unit testing,实际检查该非现有值。 但我根本不知道如何用Mockito和JUnit做到这一点。 这部分当然不起作用: @Test(expected=Exception.class) public void DoesNotExist_throwsException() throws Exception { when(TestEnum.MAYBE).thenReturn(TestEnum.MAYBE); WorkTheEnum(TestEnum.MAYBE); } 我发现了一个使用PowerMock的例子,但我无法与Mockito合作。 有任何想法吗?

我可以使用mockito来匹配具有自动更新时间戳的对象吗?

在进行模拟调用之前自动更新时间戳的最佳方法是什么? 这是我试图测试的一些虚拟代码: public class ThingWithATimestamp { public Long timestamp; public String name; public ThingWithATimestamp(String name) { this.name = name; } } public class TheClassThatDoesStuff { private ThingConnector connector; public TheClassThatDoesStuff(ThingConnector connector) { this.connector = connector; } public void updateTheThing(MyThingWithATimestamp thing) { thing.timestamp = currentTimestamp(); connector.update(thing); } } 这是我想要测试的: public class TheClassThatDoesStuffTests { @Test public void […]

Mockito可以根据方法调用时的值validation参数吗?

我有一个Foo类,它是SUT和一个Bar类,它是它的合作者。 Foo使用“ expectedList ”作为参数调用Bar上的run(List values) 。 然后, Foo将向此List添加一些元素,以使其状态与调用run()时的状态不同。 这是我的测试用例。 @Test public void testFoo() { Bar collaborator = spy(new Bar()); Foo sut = new Foo(collaborator); verify(collaborator).run(expectedList); } 请注意,协作者实际上是间谍对象而不是模拟对象。 此测试用例将失败,因为即使使用等于expectedList的参数调用run() ,它也会被修改,因为它的当前值不再等于expectedList 。 然而,这是它应该工作的方式,所以我想知道是否有办法让Mockito在调用方法时存储参数的快照,并根据这些值而不是最近的值来validation它们。

在匿名类中测试方法时,如何使用Powermockito来模拟新对象的构造?

我想写一个JUnit测试来validation下面的代码使用BufferedInputStream: public static final FilterFactory BZIP2_FACTORY = new FilterFactory() { public InputStream makeFilter(InputStream in) { // a lot of other code removed for clarity BufferedInputStream buffer = new BufferedInputStream(in); return new CBZip2InputStream(buffer); } }; (FilterFactory是一个接口。) 到目前为止我的测试看起来像这样: @Test public void testBZIP2_FactoryUsesBufferedInputStream() throws Throwable { InputStream in = mock(InputStream.class); BufferedInputStream buffer = mock(BufferedInputStream.class); CBZip2InputStream expected = mock(CBZip2InputStream.class); […]

PowerMock,模拟静态方法,然后在所有其他静态上调用真实方法

我正在设置一个类的静态方法。 我必须在@Before -annotated JUnit设置方法中执行此操作。 我的目标是设置类来调用实际方法, 除了我明确模拟的那些方法。 基本上: @Before public void setupStaticUtil() { PowerMockito.mockStatic(StaticUtilClass.class); when(StaticUtilClass.someStaticMethod(antString())).thenReturn(5); // mock out certain methods… // Now have all OTHER methods call the real implmentation??? How do I do this? } 我StaticUtilClass的问题是,在StaticUtilClass ,方法public static int someStaticMethod(String s)不幸地抛出RuntimeException如果提供了null值)。 因此,我不能简单地将调用实际方法的明显路线作为默认答案,如下所示: @Before public void setupStaticUtil() { PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static […]

如何使用Mockito / Powermock模拟枚举单例类?

我不确定如何模拟枚举单例类。 public enum SingletonObject{ INSTANCE; private int num; protected setNum(int num) { this.num = num; } public int getNum() { return num; } 我想在上面的例子中存根getNum(),但我无法弄清楚如何模拟实际的SingletonObject类。 我认为使用Powermock准备测试会有所帮助,因为枚举本身就是最终的。 //… rest of test code @Test public void test() { PowerMockito.mock(SingletonObject.class); when(SingletonObject.INSTANCE.getNum()).thenReturn(1); //does not work } 这是使用PowerMockMockito 1.4.10和Mockito 1.8.5。

形成Mockito“语法”

Mockito似乎是一个非常甜蜜的Java存根/模拟框架。 唯一的问题是我找不到关于使用其API的最佳方法的任何具体文档。 测试中使用的常用方法包括: doXXX(???) : Stubber when(T) : OngoingStubbing then(T) : OngoingStubbing verify(???) : T given(T) : BDDOngoingStubbing willXXX(???) : BDDStubber 当你在实践中看到Mockito的例子时,你会看到如下代码: when(yourMethod()).thenReturn(5); 从我读过的所有文档中,我已经确定了Mockito“语法”的几个“模式”,它们是通过菊花链式连接这些方法调用获得的,就像上面的例子一样。 我发现的一些常见模式是: 当/ Then: when(yourMethod())。thenReturn(5); 给定/将:给定(yourMethod())。willThrow(OutOfMemoryException.class); Do / When: doReturn(7).when(yourMock.fizzBu​​zz()); Will / Given / Do: willReturn(any())。given(yourMethod())。doNothing(); validation/执行:validation(yourMethod())。doThrow(SomeException.class); 我正在窒息的是如何选择正确的模式/方法调用组合来模拟我的测试用例。 看起来你可以在看似无穷无尽的组合中将它们连接起来,我不确定哪种模式适合哪个问题。 一些Mockito Guru可以帮助阐明Mockito方法的哪些模式/组合用于哪种类型的测试用例(以及为什么)? 提前致谢!

为什么Mockito没有模拟静态方法?

我在这里阅读了一些关于静态方法的线程,我想我明白了误用/过度使用静态方法会导致的问题。 但我并没有真正了解为什么很难模拟静态方法。 我知道其他嘲弄框架,比如PowerMock,可以做到这一点,但为什么不能Mockito? 我读过这篇文章 ,但作者似乎虔诚地反对static这个词,也许这是我理解不足的原因。 一个简单的解释/链接将是伟大的。