Tag: 嘲笑

mockito与jMock的州有相同的习惯用语吗?

增长面向对象软件这本书在jMock中给出了几个例子,其中状态是明确的,而不是通过API公开它。 我真的喜欢这个主意。 有没有办法在Mockito做到这一点? 这是本书的一个例子 public class SniperLauncherTest { private final States auctionState = context.states(“auction state”) .startsAs(“not joined”); @Test public void addsNewSniperToCollectorAndThenJoinsAuction() { final String itemId = “item 123”; context.checking(new Expectations() {{ allowing(auctionHouse).auctionFor(itemId); will(returnValue(auction)); oneOf(sniperCollector).addSniper(with(sniperForItem(item))); when(auctionState.is(“not joined”)); oneOf(auction).addAuctionEventListener(with(sniperForItem(itemId))); when(auctionState.is(“not joined”)); one(auction).join(); then(auctionState.is(“joined”)); }}); launcher.joinAuction(itemId); } }

使用mockito模拟嵌套方法调用

我有4个类让我们说A,B,C,D各自调用另一个类的方法。 现在我嘲笑了A类,想要使用mockito模拟一个方法 A a = Mockito.mock(A.class); 并希望在递归方法调用上得到“foo” a.getB().getC().getD()应返回”foo” 我试过了 当(a.getB()GETC()GETD()。)thenReturn( “foo” 的)。 但得到了nullPointerException 然后我试了 doReturn( “富”)时(a.getB()GETC()GETD()…)。; 然后我得到了org.mockito.exceptions.misusing.UnfinishedStubbingException: 我知道我可以创建B,C和D的对象,或者甚至可以写出类似的东西 B b = mock(B.class)或A.setB(new B()) 等等。 但我不能一次性做到这一点? 任何帮助,将不胜感激。

如何在java中模拟单个方法

是否可以模拟Java类的单个方法? 例如: class A { long method1(); String method2(); int method3(); } // in some other class class B { void someMethod(A a) { // how would I mock A.method1(…) such that a.method1() returns a value of my // choosing; // whilst leaving a.method2() and a.method3() untouched. } }

java:如何模拟Calendar.getInstance()?

在我的代码中我有这样的事情: private void doSomething() { Calendar today = Calendar.getInstance(); …. } 如何在我的junit测试中“模拟”它以返回特定的日期?