如何用void返回类型模拟方法?
我实现了一个观察者模式,但我无法用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();
}
Run Code Online (Sandbox Code Playgroud)
系统不会通过模拟触发.=(我想显示上面提到的系统状态.并根据它们做出断言.
考虑一个方法签名,如:
public String myFunction(String abc);
Run Code Online (Sandbox Code Playgroud)
Mockito可以帮助返回方法收到的相同字符串吗?
如何验证是否未在对象的依赖项上调用方法?
例如:
public interface Dependency {
void someMethod();
}
public class Foo {
public bar(final Dependency d) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
通过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??**
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个被调用两次的方法,我想捕获第二个方法调用的参数.
这是我尝试过的:
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()
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个TooManyActualInvocations例外,因为Mockito认为doSomething应该只调用一次.
如何验证第二次调用的参数doSomething?
Mockito框架@Mock和之间有什么区别@InjectMocks?
我有一个void返回类型的方法.它也可以抛出一些异常,所以我想测试那些被抛出的异常.由于同样的原因,所有尝试都失败了:
Stubber类型中的(T)方法不适用于参数(void)
任何想法如何让方法抛出指定的异常?
doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));
Run Code Online (Sandbox Code Playgroud) 有没有办法,使用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 …Run Code Online (Sandbox Code Playgroud) 我写了一个工厂来生产java.sql.Connection物品:
public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory {
@Override public Connection getConnection() {
try {
return DriverManager.getConnection(...);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想验证传递给的参数DriverManager.getConnection,但我不知道如何模拟静态方法.我正在使用JUnit 4和Mockito来测试我的测试用例.有没有一种很好的方法来模拟/验证这个特定的用例?
我正在使用Mockito 1.9.0.我想在JUnit测试中模拟一个类的单个方法的行为,所以我有
final MyClass myClassSpy = Mockito.spy(myInstance);
Mockito.when(myClassSpy.method1()).thenReturn(myResults);
Run Code Online (Sandbox Code Playgroud)
问题是,在第二行,myClassSpy.method1()实际上是被调用,导致异常.我使用模拟的唯一原因是,以后,无论什么时候myClassSpy.method1()被调用,都不会调用真正的方法并myResults返回对象.
MyClass是一个接口myInstance,如果重要的话就是它的实现.
我需要做些什么来纠正这种间谍行为?
我是Mockito的新手.
鉴于下面的类,我如何使用Mockito来验证someMethod在调用之后是否被调用了一次foo?
public class Foo
{
public void foo(){
Bar bar = new Bar();
bar.someMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
我想进行以下验证通话,
verify(bar, times(1)).someMethod();
Run Code Online (Sandbox Code Playgroud)
在哪里bar是一个模拟的实例Bar.