这是来自官方的 JMockit 教程:
@Test
public void doSomethingHandlesSomeCheckedException() throws Exception
{
new Expectations() {
DependencyAbc abc;
{
abc.stringReturningMethod();
returns("str1", "str2");
result = new SomeCheckedException();
}
};
new UnitUnderTest().doSomething();
}
是否有可能陈述相反的情况,即多个结果和一个返回 - 我需要抛出 2 个异常,然后才返回一个好的值。我正在寻找这样的东西:
abc.stringReturningMethod();
returns(new SomeCheckedException(), new SomeOtherException(),"third");
这不起作用,JMockit 无法将这些异常转换为 String
(这是 stringReturningMethod
的返回类型)
最佳答案
这样写:
abc.stringReturningMethod();
result = new SomeCheckedException();
result = new SomeOtherException();
result = "third";
关于java - JMockit 多个异常作为方法调用的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318094/