java EasyMock 异常处理

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

EasyMock Exception Handling

javajuniteasymock

提问by Walls

I am creating some junit test cases using EasyMock. So far it makes sense for classes I am expecting to return POJOs, but how should I handle dealing with a DAO object that itself could throw an exception. My test case is to check for an expected exception thrown when the DAO encounters an issue. Using EasyMock I try and mock the DAO object (testing from the fooclass), what is the correct way to handle the lower level DAO exception.

我正在使用 EasyMock 创建一些 junit 测试用例。到目前为止,这对于我期望返回 POJO 的类是有意义的,但是我应该如何处理本身可能引发异常的 DAO 对象。我的测试用例是检查当 DAO 遇到问题时抛出的预期异常。使用 EasyMock 我尝试模拟 DAO 对象(从foo类中测试),处理较低级别 DAO 异常的正确方法是什么。

An example of the classes/simple calls is below: (Assume all getters/setters/constructors are valid and present)

类/简单调用的示例如下:(假设所有 getter/setter/constructors 都有效且存在)

public class foo{
    private daoClass dao = daoClass.getInstance();
    public String getValueFromDB(String key) throws DBException{
        return dao.lookup(key);
    }
}

public class daoClass{ //singleton DAO 
    public daoClass getInstance(){
       //singleton access here
    }
    public String lookup(String key) throws DBException{
        try{
            //DB LOGIC
        }
        catch(Exception e){
            throw new DBException(e.getMessage());
        }
    }
}

When I try and test the fooclass, I want to be able to test for this DBException. How do I handle this, should I be surronding the DAO call in a try/catch (in the test), or add a throws to the test? I know expected=DBExceptionwill pass the test if that is thrown, but how syntactically should you handle any number of inner exceptions?

当我尝试测试这个foo类时,我希望能够测试这个 DBException。我该如何处理这个问题,我应该在 try/catch 中(在测试中)包围 DAO 调用,还是在测试中添加一个 throws?我知道expected=DBException如果抛出它会通过测试,但是你应该如何处理任意数量的内部异常?

Test code Example:

测试代码示例:

@Test(expected=DBException.class)
public void testFooError(){
    String key = "test";
    String value = "expected";
    daoClass daoMock = createMock(daoClass.class);
    try{
        expect(daoMock.lookup(key)).andReturn(value);
    } catch (DBException e){
        // ???
    }
}

What is the correct way to handle when the expectcould potentially throw errors? Should the test method throwthe exception, or should a try/catchbe used? Is it still correct to use the expected=EXCEPTIONtag on the test?

expect可能引发错误时,正确的处理方法是什么?测试方法throw应该是异常,还是应该try/catch使用?expected=EXCEPTION在测试中使用标签仍然正确吗?

回答by combinatorics

Here's how I handle exceptions in unit tests:

以下是我在单元测试中处理异常的方法:

If you aren'texplicitly testing for the exception then you should add a throws clause to the method - if the exception was thrown and you weren't expecting it to be thrown then that's a test fail. e.g.,

如果您没有显式地测试异常,那么您应该向该方法添加一个 throws 子句 - 如果抛出了异常并且您不希望它被抛出,那么这就是测试失败。例如,

@Test
public void testFooNormal() throws DBException{
    String key = "test";
    String value = "expected";
    daoClass daoMock = createMock(daoClass.class);
    expect(daoMock.lookup(key)).andReturn(value);
    // do an assert on returned value
    ...
}

If you areexplicitly testing for the exception then put a try-catch around the line you expect it to be thrown from (catching the narrowest version of the Exception you expect) and then set a boolean in the catch clause and the assert should be on the value of the boolean. e.g.,

如果您正在显式测试异常,则在您希望抛出它的行周围放置一个 try-catch(捕获您期望的异常的最窄版本),然后在 catch 子句中设置一个布尔值,并且断言应该打开布尔值。例如,

@Test
public void testFooError(){
    String key = "test";
    String value = "expected";
    boolean exceptionThrown = false;
    daoClass daoMock = createMock(daoClass.class);
    try{
      expect(daoMock.lookup(key)).andReturn(value);
    }catch (DBException e) {
      exceptionThrown = true;
    }
    // assert exceptionThrown is true
    ...
}

This is a good way to test exceptions because it means you are testing not only that the correct exception is thrown but also that it's thrown from exactly the line you expect. If you use @test(expected=...) then a different line in the test could throw that exception and the test could incorrectly pass.

这是测试异常的好方法,因为这意味着您不仅要测试是否抛出了正确的异常,而且还要测试它是否恰好从您期望的那一行抛出。如果您使用 @test(expected=...) 那么测试中的另一行可能会抛出该异常并且测试可能会错误地通过。

回答by Avinash Singh

Your DAO logic should not change based on your test . If you are expecting your DAO to throw the exception then keep it that way and test the Exception in the way you are testing .

您的 DAO 逻辑不应根据您的测试而改变。如果您希望您的 DAO 抛出异常,那么保持这种方式并以您正在测试的方式测试异常。

If you are catching the exception in DAO itself to do rollback or logging etc. , then the Test Case shouldn't expect Exception as it is not a test case scenario. You may check for NULL in this case as I expect your DAO will be returning NULL .

如果您在 DAO 本身中捕获异常以进行回滚或日志记录等,那么测试用例不应期待异常,因为它不是测试用例场景。在这种情况下,您可以检查 NULL ,因为我希望您的 DAO 将返回 NULL 。