java 使用 EasyMock 测试方法的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12494003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:08:41  来源:igfitidea点击:

Testing Exceptions of a method with EasyMock

javaexceptiontestngeasymock

提问by Sam ツ

I am newbie to unit testing. I am using TestNG with MyEclipse to develop unit test cases for my application. While doing it I am facing some problems with EasyMock. Here is my code (Name of the class, method names and return types are changed for security reasons but you will get a clear idea what I am trying to achieve here).

我是单元测试的新手。我在 MyEclipse 中使用 TestNG 来为我的应用程序开发单元测试用例。在这样做时,我遇到了 EasyMock 的一些问题。这是我的代码(出于安全原因更改了类的名称、方法名称和返回类型,但您将清楚地了解我在这里要实现的目标)。

    public MyClass
    {
       // This is a method in my class which calls a collaborator which I
       // want to mock in my test case
       public SomeObject findSomething(SomeOtherObject param) throws Exception
       {
          SomeOtherObject param a = myCollaborator.doSomething(param);
          // Do something with the object and then return it 
          return a;
       }
    }

Now here is my test. Now what I actually want to achieve in my test case is that I want to check that my function (findSomething) properly throws exception in case some exception is thrown. In future some other developer can change the signature (throws Exception isn't really part of method signature) of the method and remove the throws Exception from my method. So how can I make sure that nobody changes it?

现在这是我的测试。现在我在我的测试用例中真正想要实现的是我想检查我的函数(findSomething)是否正确抛出异常,以防抛出一些异常。将来,其他一些开发人员可以更改方法的签名(throws Exception 实际上不是方法签名的一部分)并从我的方法中删除 throws Exception。那么我怎样才能确保没有人改变它呢?

@Test(dataProvider="mydataProvider", expectedExceptions=Exception.class)
public void MyTest(SomeOtherObject param) throws Exception {
{
  EasyMock.expect(myCollaboratorMock.doSomething(param)).andThrow(new Exception());
  EasyMock.replay(myCollaboratorMock);
}

I am getting exception

我得到了例外

"java.lang.IllegalArgumentException: last method called on mock cannot throw java.lang.Exception"

“java.lang.IllegalArgumentException:在模拟上调用的最后一个方法不能抛出java.lang.Exception”

What I am doing wrong here? Can someone shed some light on how to write a test case for my particular scenario?

我在这里做错了什么?有人可以说明如何为我的特定场景编写测试用例吗?

回答by JB Nizet

The collaborator's doSomething()method doesn't declare that it may throw Exception, and you're telling its mock to throw one. It's not possible.

合作者的doSomething()方法没有声明它可能会抛出异常,并且你告诉它的模拟抛出一个。这是不可能的。

Exception is a checked exception. It can only be thrown if it's declared in the method signature. If the method has no throwsclause, all it can do is throwing runtime exceptions (i.e. RuntimeExceptionor any descendant class).

Exception 是一个已检查的异常。如果它在方法签名中声明,则只能抛出它。如果该方法没有throws子句,它所能做的就是抛出运行时异常(即RuntimeException或任何后代类)。