JUnit 测试异常 [重复]

Posted

技术标签:

【中文标题】JUnit 测试异常 [重复]【英文标题】:JUnit Testing Exceptions [duplicate] 【发布时间】:2013-02-19 10:04:02 【问题描述】:

我真的是java新手。

我正在对构造函数运行一些 JUnit 测试。构造函数是这样的,如果它的参数之一被赋予 null 或空字符串,它应该抛出异常。

当我在 JUnit 中使用 null 或空字符串参数测试此构造函数时,我得到一个红色条,尽管我几乎 100% 确定构造函数方法确实在将此类参数传递给时抛出异常它。

如果方法以应有的方式抛出异常,JUnit 中不应该有一个绿色条吗?还是当异常抛出按应有的方式工作时,您应该得到一个红色条?

【问题讨论】:

【参考方案1】:
@Test(expected = Exception.class)  

告诉 Junit 异常是预期的结果,因此当抛出异常时测试将通过(标记为绿色)。

对于

@Test

如果抛出异常,Junit 会将测试视为失败,如果它是未经检查的异常。如果检查了异常,它将无法编译,您将需要使用其他方法。 这个link 可能会有所帮助。

【讨论】:

但是如何通过此解决方案传递自定义消息?我的意思是业务逻辑是:throw IOException("username can't be null")。我知道org.junit.rules.ExpectedException,但我不想创建实例并使用.expect(.expectMessage(【参考方案2】:

你确定你告诉它期待异常吗?

对于较新的 junit (>= 4.7),您可以使用类似 (from here)

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

@Test
public void testRodneCisloRok()
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("error1");
    new RodneCislo("891415",dopocitej("891415"));

对于较旧的junit,这是:

@Test(expected = ArithmeticException.class)  
public void divisionWithException()   
  int i = 1/0;

【讨论】:

如果测试类抛出异常,你可以简单的抛出异常,在你写Junit测试用例的地方进行测试。使用 @Test(expected = IllegalArgumentException.class)【参考方案3】:

如果你的构造函数和这个类似:

public Example(String example) 
    if (example == null) 
        throw new NullPointerException();
    
    //do fun things with valid example here

然后,当你运行这个 JUnit 测试时,你会得到一个绿色条:

@Test(expected = NullPointerException.class)
public void constructorShouldThrowNullPointerException() 
    Example example = new Example(null);

【讨论】:

【参考方案4】:

使用 ExpectedException Rule(4.7 版)的一个好处是您可以测试异常消息,而不仅仅是预期的异常。

并且使用 Matchers,您可以测试您感兴趣的消息部分:

exception.expectMessage(containsString("income: -1000.0"));

【讨论】:

【参考方案5】:

虽然@Test(expected = MyException.class) 和ExpectedException rule 是非常好的选择,但在某些情况下,JUnit3 风格的异常捕获仍然是最好的选择:

@Test public void yourTest() 
  try 
    systemUnderTest.doStuff();
    fail("MyException expected.");
   catch (MyException expected) 

    // Though the ExpectedException rule lets you write matchers about
    // exceptions, it is sometimes useful to inspect the object directly.

    assertEquals(1301, expected.getMyErrorCode());
  

  // In both @Test(expected=...) and ExpectedException code, the
  // exception-throwing line will be the last executed line, because Java will
  // still traverse the call stack until it reaches a try block--which will be
  // inside the JUnit framework in those cases. The only way to prevent this
  // behavior is to use your own try block.

  // This is especially useful to test the state of the system after the
  // exception is caught.

  assertTrue(systemUnderTest.isInErrorState());

另一个声称在这里提供帮助的库是catch-exception;然而,截至 2014 年 5 月,该项目似乎处于维护模式(被 Java 8 淘汰),并且很像 Mockito 捕获异常只能操作非final 方法。

【讨论】:

以上是关于JUnit 测试异常 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Java Junit 中测试异常 [重复]

[JUnit] JUnit5 基础 4 - Maven 集成,参数化测试,重复测试 [完]

junit测试中的Java.lang.nullpointerexception [重复]

JUnit @Before和@After在每次测试之前和之后执行[重复]

用MyEclipse测试Hibernate的Junit方法测试总是重复执行的原因

Junit测试具有数据库访问权限的多线程应用程序[重复]