JUnit 4预期的异常类型 [英] JUnit 4 Expected Exception type

查看:175
本文介绍了JUnit 4预期的异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对其他人编写的代码进行JUnit测试,但我无法弄清楚如何测试异常,因为异常似乎缺少类型。

I am trying to do a JUnit test on code that someone else has written, but I cannot figure out how to test for the exception, because the exception seems to lack a type.

public Pirate(String name, int initialGold) throws Exception {
    if(initialGold < 0)
        throw new Exception("Init Gold must be >= 0");
    this.name = name;
    this.numGold = initialGold;
    this.health = Pirate.DEFAULT_HEALTH;
    this.isCursed = false;
}

My JUnit Code snippet:

My JUnit Code snippet:

@Test
public static void constructorTest() throws Exception{
    rodgers = new Pirate("Dread Pirate Rodgers", 10000);
    assertEquals("Dread Pirate Rodgers" , rodgers.getName());
    assertEquals(10000, rodgers.getNumGold());
    assertEquals(100, rodgers.getHealth());
    assertEquals(false, rodgers.getIsCursed());
}

@Test()
public static void exceptionTest() throws Exception{
    rodgers = new Pirate("Dread Pirate Rodgers" , -100);

}

我知道我需要把预期=(某种类型的异常)在测试的括号中,但我对异常类型一无所知。

I know I need to put expected = (some type of exception) in the parenthesis of test, but I am clueless as to the exception type.

推荐答案

实际上有一种替代 @Test(expected = Xyz.class)在JUnit 4.7中使用 规则 ExpectedException

There's actually an alternative to the @Test(expected=Xyz.class) in JUnit 4.7 using Rule and ExpectedException

在您的测试用例中,您声明 ExpectedException 使用 @Rule 进行注释,并为其指定默认值 ExpectedException.none()。然后在您期望异常的测试中,将值替换为实际预期值。这样做的好处是,如果不使用丑陋的try / catch方法,您可以进一步指定异常中的消息是什么

In your test case you declare an ExpectedException annotated with @Rule, and assign it a default value of ExpectedException.none(). Then in your test that expects an exception you replace the value with the actual expected value. The advantage of this is that without using the ugly try/catch method, you can further specify what the message within the exception was

@Rule public ExpectedException thrown= ExpectedException.none();

@Test
public void myTest() {
    thrown.expect( Exception.class );
    thrown.expectMessage("Init Gold must be >= 0");

    rodgers = new Pirate("Dread Pirate Rodgers" , -100);
}

使用此方法,您可以测试通用中的消息异常是特定的。

Using this method, you might be able to test for the message in the generic exception to be something specific.

ADDITION
使用 ExpectedException 是您可以在测试用例的上下文中更准确地确定异常的范围。如果您只在测试中使用 @Test(expected = Xyz.class)注释,则可以在测试代码中的任何位置抛出Xyz异常 - 包括任何测试设置或在测试方法中预先断言。这会导致误报。

ADDITION Another advantage of using ExpectedException is that you can more precisely scope the exception within the context of the test case. If you are only using @Test(expected=Xyz.class) annotation on the test, then the Xyz exception can be thrown anywhere in the test code -- including any test setup or pre-asserts within the test method. This leads to a false positive.

使用ExpectedException,您可以推迟指定 thrown.expect(Xyz.class)直到任何设置和预先断言之后,就在实际调用测试方法之前。因此,您可以更准确地确定实际方法调用而不是任何测试夹具本身抛出的异常。

Using ExpectedException, you can defer specifying the thrown.expect(Xyz.class) until after any setup and pre-asserts, just prior to actually invoking the method under test. Thus, you more accurately scope the exception to be thrown by the actual method invocation rather than any of the test fixture itself.

JUnit 5注意:

JUnit 5删除了 @Test(expected = ...) @规则 ExpectedException 。它们被替换为新的 assertThrows() ,这需要使用Java 8和lambda语法。

JUnit 5 has removed @Test(expected=...), @Rule and ExpectedException altogether. They are replaced with the new assertThrows(), which requires the use of Java 8 and lambda syntax.

这篇关于JUnit 4预期的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆