简体   繁体   English

JUnit4:测试预期的异常

[英]JUnit4 : testing for expected exception

I'm trying to test with JUnit4 a method throwing an exception. 我试图用JUnit4测试抛出异常的方法。 Here is the code snippet : 这是代码片段:

package unit_tests;

import org.junit.Test;
import calculator.*;

@Test(expected=CalcError.class)
public void testDivision() {
    Calculator myCalc = new Calculator(10, 0);
    myCalc.setOperation(Calculator.Operation_e.DIVIDE);
    myCalc.getResult();
}

The problem is with the line @Test(expected=CalcError.class) : I get the following error : 问题出在@Test(expected=CalcError.class) :我收到以下错误:

Class<CalcError> cannot be resolved to a type

Here is how CalcError is defined : 以下是CalcError的定义方式:

package calculator;

public class Calculator {
    public class CalcError extends Exception {
        // ...
    }

    public double getResult() throws CalcError {
        // ...
    }
}

I don't understand why CalcError is not a type, even though the unit tests are in a unit_tests package and the calculator is in a calculator package. 我不明白为什么CalcError不是一个类型,即使单元测试在unit_tests包中并且计算器在calculator包中。

What am I missing ? 我错过了什么?

CalcError is an inner class, so you need to use CalcError是一个内部类,所以你需要使用

@Test(expected=Calculator.CalcError.class)

See Nested Classes . 请参阅嵌套类

EDIT: You will need to declare the test method as throwing Calculator.CalcError as well: 编辑:你需要将测试方法声明为抛出Calculator.CalcError:

@Test(expected=Calculator.CalcError.class)
public void testDivision() throws Calculator.CalcError {
    Calculator myCalc = new Calculator(10, 0);
    myCalc.setOperation(Calculator.Operation_e.DIVIDE);
    myCalc.getResult();
}

This is to please the compiler, because Calculator.CalcError is an checked Exception. 这是为了取悦编译器,因为Calculator.CalcError是一个经过检查的异常。

CalcError is an inner class of Calculator . CalcErrorCalculator的内部类。 It is not imported by import calculator.*; 它不是由import calculator.*; You'd have to add import calculator.Calculator.CalcError or qualify CalcError ( expected=Calculator.CalcError.class ). 您必须添加import calculator.Calculator.CalcError或限定CalcErrorexpected=Calculator.CalcError.class )。

Having public classes as inner classes is not a good idea. 将公共课作为内部课程并不是一个好主意。 You should move that to its own file. 您应该将其移动到自己的文件中。 Only private classes should be nested. 只应嵌套私有类。 You could probably access the Error class by using Calculator.CalcError.class but I'd strong advise against it. 您可以通过使用Calculator.CalcError.class访问Error类,但我强烈建议不要使用它。

Apart from that I think JUnit lacks a bit of exception detection capabilities as you cannot set a message. 除此之外,我认为JUnit缺乏一些异常检测功能,因为您无法设置消息。 I unit test for exceptions by catching them and then calling Assert.fail after the method call that should throw an exception: 我通过捕获它们然后在应该抛出异常的方法调用之后调用Assert.fail来对异常进行单元测试:

try {
    someMethod();
    Assert.fail("SomeException should have been thrown");
catch(SomeException se) {
}

Since CalcError is an inner class to Calculator you need to reference it like this 由于CalcError是Calculator的内部类,因此您需要像这样引用它

@Test(expected=Calculator.CalcError.class)
public void testDivision() {

由于这是一个经过检查的异常,您需要向方法签名添加throws Exception ,否则编译器会抱怨异常。

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

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