从 Mockito 模拟中抛出异常
有些东西告诉我我做错了什么......它不应该这么难。
我有一个依赖于某些内部类的类。我通过受保护的方法创建该内部,以便我可以在测试中覆盖它以提供模拟。我知道要让 Mockito 模拟对象从 void 方法抛出异常,我必须使用 doThrow。
但我的问题是编译器抱怨对 Mockito.doThrow() 的调用抛出 RuntimeException...所以我必须在设置模拟的方法中添加一个 throws 子句。这是 Mockito 本身的错误吗? doThrow 正在声明将来应该发生的事情,但不是在模拟设置期间发生。
我的 OuterClass 看起来像...
public class OuterClass {
protected InnerClass inner;
protected void createInner() { inner = new InnerClass(); }
protected doSomething() {
try {
inner.go();
} catch (RuntimeException re) {
throw new MyException("computer has caught fire");
}
}
}
而我的测试代码看起来像...
@Test(expected = MyException.class)
public void testSomething() {
OuterClass outer = new TestOuterClass();
outer.doSomething();
}
public static class TestOuterClass extends OuterClass {
@Override
public void createInner() {
InnerClass mock = Mockito.mock(InnerClass.mock);
Mockito.doThrow(new RuntimeException("test")).when(mock).go(); // PROBLEM
inner = mock;
}
}
Something tells me I'm doing something wrong...it shouldn't be this hard.
I have a class that relies on some inner class. I'm creating that inner through a protected method so that I can override that in a test to provide a mock. I understand that to get a Mockito mock object to throw an exception from a void method, I have to use doThrow.
But my problem is that the compiler complains that the call to Mockito.doThrow() is throwing RuntimeException...so then I have to add a throws clause to the method setting up the mock. Is that a bug in Mockito itself? doThrow is declaring something that should happen in the future, but not during the setup of the mock.
My OuterClass looks like...
public class OuterClass {
protected InnerClass inner;
protected void createInner() { inner = new InnerClass(); }
protected doSomething() {
try {
inner.go();
} catch (RuntimeException re) {
throw new MyException("computer has caught fire");
}
}
}
And my test code looks like...
@Test(expected = MyException.class)
public void testSomething() {
OuterClass outer = new TestOuterClass();
outer.doSomething();
}
public static class TestOuterClass extends OuterClass {
@Override
public void createInner() {
InnerClass mock = Mockito.mock(InnerClass.mock);
Mockito.doThrow(new RuntimeException("test")).when(mock).go(); // PROBLEM
inner = mock;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请您检查一下编译器是否真的在抱怨
RuntimeException
?关于 RuntimeException 的问题是,它在编译时应该无关紧要。我怀疑编译器实际上是在抱怨
MyException
- 我认为这是编译器注意到的检查异常,它是由doSomething()
方法抛出的。您应该能够简单地将throws MyException
添加到测试方法,然后测试运行程序将捕获该方法。正如预期的那样,测试运行程序将通过。Please could you check that the compiler really is complaining about a
RuntimeException
?The thing about
RuntimeException
is that it shouldn't matter at compile time. I suspect the compiler is actually complaining aboutMyException
- which I presume is a checked exception, noticed by the compiler, which is thrown by thedoSomething()
method. You should be able to simply add thethrows MyException
to the test method, which will then be caught by the test runner. Since it's expected, the test runner will pass.