【发布时间】:2013-03-23 01:33:49
【问题描述】:
这是我想强制抛出远程异常的代码:
transient Bicycle b=null;
public Bicycle getBicycle() {
if(b==null) {
try {
b=new Bicycle(this);
} catch (RemoteException ex) {
Logger.getLogger(Bicycle()).log(Level.SEVERE, null, ex);
}
}
return b;
}
这是我使用 Mockito 运行的 JUnit 测试:
boolean exceptionThrown=false;
Bicycle mockB = mock(Bicycle);
mockB.setBicycle(null);
stub(mockB.getBicycle()).toThrow(new RemoteException(){boolean exceptionThrown = true;});
assertTrue(exceptionThrown);
我不断收到以下错误:
Checked exception is invalid for this method!
我们将不胜感激。
编辑:
代替
stub(mockB.getBicycle()).toThrow(new RemoteException(){boolean exceptionThrown = true;});
我也试过
doThrow(new RemoteException(){boolean exceptionThrown = true;}).when(mockB).getBicycle();
和
Mockito.when(mockB.getBicycle()).thenThrow(new RemoteException(){boolean exceptionThrown=true;});
还是没有运气。
Edit2 - 在完全理解 API 并正确使用之后更进一步:
when(mockB.getBicycle()).thenThrow(new RuntimeException());
我现在不知道如何进行断言。一旦异常被调用,我尝试放置一个布尔值,但断言看不到布尔值。
有什么想法吗?
【问题讨论】:
-
Bicycle的构造函数是否声明它抛出RemoteException? -
不,我想我现在明白我的问题了,我有一个稍微不同的问题,我不知道如何让我的断言正确。我将进行编辑。
标签: java junit mockito remoteexception