java 如何强制方法在 jUnit 测试中抛出异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11565196/
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 05:36:12  来源:igfitidea点击:

How to force a method to throw an Exception in jUnit testing?

javajakarta-eetestingjunit

提问by Incognito

Actually this is for code coverage and I'm having a hard time, covering to catchstatements. Any ideas?

实际上这是为了代码覆盖率,我很难覆盖到catch语句。有任何想法吗?

for example:

例如:

I want my selectSomethingBySomething()method (which selects from db) to throw an SQLExceptionwhich is pretty hard on a test method without actually touching the actual source code. Given also the constraint that what I can only control is the parameters for the WHEREclause.

我希望我的selectSomethingBySomething()方法(从 db 中选择)SQLException在不实际接触实际源代码的情况下抛出一个对测试方法来说非常困难的方法。还考虑到我只能控制的约束是WHERE子句的参数。

回答by Tomasz Nurkiewicz

You need to first mock the class containing selectSomethingBySomething()and then record this behavior. In mockitoyou'll say:

您需要先模拟包含的类selectSomethingBySomething(),然后记录此行为。在mockito你会说:

SomeDao someDaoMock = mock(SomeDao.class);

willThrow(new SQLException())).given(someDaoMock).selectSomethingBySomething();

Then inject someDaoMockinto your class under test and when it calls someDaoMock.selectSomethingBySomething()it'll throw previously chosen exception.

然后注入someDaoMock您的被测类,当它调用时,someDaoMock.selectSomethingBySomething()它会抛出先前选择的异常。

回答by Jonathan S. Fisher

You need a mocking framework like Mockito. Check out the easy-to-read refcard here:

你需要一个像 Mockito 这样的模拟框架。在此处查看易于阅读的 refcard:

http://refcardz.dzone.com/refcardz/mockito

http://refcardz.dzone.com/refcardz/mockito

回答by kartheek

Using EasyMock you can mock an Class and the method calls of that particular Class.

使用 EasyMock,您可以模拟一个类和该特定类的方法调用。

For example:

例如:

If DoSomethingDAO is the ClassName,

如果 DoSomethingDAO 是类名,

Mock the class like DoSomethingDAO mockDAO

像 DoSomethingDAO mockDAO 一样模拟类

EasyMock.createMock(DoSomethingDAO.class);

now that you have to mock all the calls that are being made from this mockDAO.

现在你必须模拟从这个 mockDAO 发出的所有调用。

if the method returns a value we need to mock the call like below.

如果该方法返回一个值,我们需要模拟如下调用。

EasyMock.expect(mockDAO.selectSomethingBySomething()).andReturn(EasyMock.anyObject());

If the method throws an exception below is the method call

如果方法抛出异常,下面是方法调用

EasyMock.expect(mockDAO.selectSomethingBySomething()).andThrow(new (typeofExecption));

for void methods

对于 void 方法

mockDAO.selectSomethingBySomething();
EasyMock.expectLastCall().atleastOnce();

回答by Chris

You actually do not need an additional Framework to do the Trick. Here is how:

您实际上不需要额外的框架来执行该技巧。方法如下:

    Exception hasToBeThrown = null;
    try {
                 methodThatShouldThrow();
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("if you want to...")); // JUnit will miss this, if not thrown, so we need more
        hasToBeThrown = e;
    }
    assertNotNull(hasToBeThrown); // but this will do

回答by ???v?т?

There are lots of ways do it, but my advice is don't aim for 100% code coverage. It simply isn't worth it. You're making life difficult for yourself for negligible benefit.

有很多方法可以做到,但我的建议是不要以 100% 的代码覆盖率为目标。这根本不值得。你为了微不足道的好处而让自己生活困难。

However, if you really want to test it, probably the best way is to alter your code to make it more testable. Make your data-access class implement an interface, then use a mocking framework (such as JMock) to mock it out and throw a SQLException. (You can mock concrete classes but that is frowned on.)

但是,如果您真的想测试它,最好的方法可能是更改您的代码以使其更易于测试。使您的数据访问类实现一个接口,然后使用模拟框架(例如 JMock)将其模拟出来并抛出 SQLException。(你可以模拟具体的类,但这是不赞成的。)