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

查看:39
本文介绍了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;
}

我的 JUnit 代码片段:

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);

}

我知道我需要将 expected =(某种类型的异常)放在 test 的括号中,但我对异常类型一无所知.

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.

推荐答案

实际上在 JUnit 4.7 中使用 RuleExpectedException

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

在您的测试用例中,您声明了一个用 @Rule 注释的 ExpectedException,并为其分配一个默认值 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.

添加使用 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 can lead 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 JUnit Jupiter 已完全删除 @Test(expected=...)@RuleExpectedException.它们被替换为新的 assertThrows(),这需要使用 Java 8 和 lambda 语法.ExpectedException 仍然可以通过 JUnit Vintage 在 JUnit 5 中使用.此外,JUnit Jupiter 还将通过使用 junit-jupiter-migrationsupport 模块,但前提是您添加了 @EnableRuleMigrationSupport.

JUnit 5 JUnit Jupiter 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. ExpectedException is still available for use in JUnit 5 through JUnit Vintage. Also JUnit Jupiter will also continue to support JUnit 4 ExpectedException through use of the junit-jupiter-migrationsupport module, but only if you add an additional class-level annotation of @EnableRuleMigrationSupport.

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

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