从 Mockito 模拟中抛出异常

发布于 2024-11-01 05:46:23 字数 1057 浏览 1 评论 0原文

有些东西告诉我我做错了什么......它不应该这么难。

我有一个依赖于某些内部类的类。我通过受保护的方法创建该内部,以便我可以在测试中覆盖它以提供模拟。我知道要让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-11-08 05:46:23

请您检查一下编译器是否真的在抱怨 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 about MyException - which I presume is a checked exception, noticed by the compiler, which is thrown by the doSomething() method. You should be able to simply add the throws MyException to the test method, which will then be caught by the test runner. Since it's expected, the test runner will pass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文