我是Mockito的新手。
给定下面的类,我如何使用Mockito来验证someemethod在foo被调用后恰好被调用一次?
public class Foo
{
public void foo(){
Bar bar = new Bar();
bar.someMethod();
}
}
我想打电话确认一下,
verify(bar, times(1)).someMethod();
其中bar是bar的模拟实例。
我是Mockito的新手。
给定下面的类,我如何使用Mockito来验证someemethod在foo被调用后恰好被调用一次?
public class Foo
{
public void foo(){
Bar bar = new Bar();
bar.someMethod();
}
}
我想打电话确认一下,
verify(bar, times(1)).someMethod();
其中bar是bar的模拟实例。
我使用的是Mockito 1.9.0。我想在JUnit测试中模拟类的单个方法的行为,所以我有
final MyClass myClassSpy = Mockito.spy(myInstance);
Mockito.when(myClassSpy.method1()).thenReturn(myResults);
问题是,在第二行中,myClassSpy.method1()实际上被调用了,导致了一个异常。我使用模拟的唯一原因是,以后无论何时调用myClassSpy.method1(),都不会调用真正的方法,并且将返回myResults对象。
MyClass是一个接口,myInstance是它的实现。
我需要做什么来纠正这种间谍行为?
我写了一个工厂来产生java.sql.Connection对象:
public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory {
@Override public Connection getConnection() {
try {
return DriverManager.getConnection(...);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
我想验证传递给DriverManager的参数。getConnection,但我不知道如何模拟静态方法。我的测试用例使用JUnit 4和Mockito。是否有一个好的方法来模拟/验证这个特定的用例?
我有一个void返回类型的方法。它还可以抛出一些异常,所以我想测试那些被抛出的异常。所有的尝试都失败了,原因都一样
Stubber类型中的(T)不适用于参数(void)时的方法。
有什么想法,我可以让方法抛出一个指定的异常?
doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));
我有一个方法被调用了两次,我想捕获第二次方法调用的参数。
以下是我的尝试:
ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class);
ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class);
verify(mockBar).doSomething(firstFooCaptor.capture());
verify(mockBar).doSomething(secondFooCaptor.capture());
// then do some assertions on secondFooCaptor.getValue()
但是我得到了一个TooManyActualInvocations异常,因为Mockito认为doSomething应该只被调用一次。
如何验证doSomething的第二次调用的参数?
是否有任何方法,使用Mockito,模拟类中的一些方法,而不是其他方法?
例如,在这个(显然是虚构的)Stock类中,我想模拟getPrice()和getQuantity()返回值(如下面的测试片段所示),但我想让getValue()执行在Stock类中编码的乘法运算
public class Stock {
private final double price;
private final int quantity;
Stock(double price, int quantity) {
this.price = price;
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public double getValue() {
return getPrice() * getQuantity();
}
@Test
public void getValueTest() {
Stock stock = mock(Stock.class);
when(stock.getPrice()).thenReturn(100.00);
when(stock.getQuantity()).thenReturn(200);
double value = stock.getValue();
// Unfortunately the following assert fails, because the mock Stock getValue() method does not perform the Stock.getValue() calculation code.
assertEquals("Stock value not correct", 100.00*200, value, .00001);
}
在Mockito框架中@Mock和@InjectMocks之间有什么区别?
如何验证方法未在对象的依赖项上调用?
例如:
public interface Dependency {
void someMethod();
}
public class Foo {
public bar(final Dependency d) {
...
}
}
通过Foo测试:
public class FooTest {
@Test
public void dependencyIsNotCalled() {
final Foo foo = new Foo(...);
final Dependency dependency = mock(Dependency.class);
foo.bar(dependency);
**// verify here that someMethod was not called??**
}
}
考虑如下方法签名:
public String myFunction(String abc);
Mockito可以帮助返回与方法接收到的字符串相同的字符串吗?
如何模拟方法与无效返回类型?
我实现了一个观察者模式,但我不能用Mockito模拟它,因为我不知道怎么做。
我试图在网上找到一个例子,但没有成功。
我的类是这样的:
public class World {
List<Listener> listeners;
void addListener(Listener item) {
listeners.add(item);
}
void doAction(Action goal,Object obj) {
setState("i received");
goal.doAction(obj);
setState("i finished");
}
private string state;
//setter getter state
}
public class WorldTest implements Listener {
@Test public void word{
World w= mock(World.class);
w.addListener(this);
...
...
}
}
interface Listener {
void doAction();
}
系统不会被mock触发。
我想展示上述系统的状态。并据此断言。