简体   繁体   English

如何在jUnit测试中强制方法引发异常?

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

Actually this is for code coverage and I'm having a hard time, covering to catch statements. 实际上,这是为了覆盖代码,我很难理解catch语句。 Any ideas? 有任何想法吗?

for example: 例如:

I want my selectSomethingBySomething() method (which selects from db) to throw an SQLException which is pretty hard on a test method without actually touching the actual source code. 我希望我的selectSomethingBySomething()方法(从db中选择)抛出SQLException ,这在测试方法上很难,而无需实际触摸实际的源代码。 Given also the constraint that what I can only control is the parameters for the WHERE clause. 还给出了我只能控制的约束是WHERE子句的参数。

You need to first mock the class containing selectSomethingBySomething() and then record this behavior. 您需要首先模拟包含selectSomethingBySomething()的类,然后记录此行为。 In you'll say: 您将说:

SomeDao someDaoMock = mock(SomeDao.class);

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

Then inject someDaoMock into your class under test and when it calls someDaoMock.selectSomethingBySomething() it'll throw previously chosen exception. 然后将someDaoMock注入正在测试的类中,当它调用someDaoMock.selectSomethingBySomething()它将抛出先前选择的异常。

You need a mocking framework like Mockito. 您需要一个类似Mockito的模拟框架。 Check out the easy-to-read refcard here: 在此处查看易于阅读的refcard:

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

Using EasyMock you can mock an Class and the method calls of that particular Class. 使用EasyMock,您可以模拟一个类以及该特定类的方法调用。

For example: 例如:

If DoSomethingDAO is the ClassName, 如果DoSomethingDAO是ClassName,

Mock the class like DoSomethingDAO mockDAO 像DoSomethingDAO模拟类那样模拟类

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 用于无效方法

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

There are lots of ways do it, but my advice is don't aim for 100% code coverage. 有很多方法可以做到这一点,但是我的建议不是针对100%的代码覆盖率。 It simply isn't worth it. 根本不值得。 You're making life difficult for yourself for negligible benefit. 您因微不足道的利益而使自己生活困难。

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. 使您的数据访问类实现一个接口,然后使用模拟框架(例如JMock)对其进行模拟并抛出SQLException。 (You can mock concrete classes but that is frowned on.) (您可以嘲笑具体的类,但是对此不满意。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM