JUnit 测试异常

我对爪哇真的很陌生。

我正在对一个构造函数运行一些 JUnit 测试。构造函数是这样的: 如果为它的一个参数提供一个 null 或空字符串,它就应该抛出一个异常。

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

如果方法按照预期的方式抛出异常,那么 JUnit 中是否应该有一个绿色的条?还是当抛出异常按照预期的方式工作时,应该得到一个红色条?

145540 次浏览
@Test(expected = Exception.class)

Tells Junit that exception is the expected result so test will be passed (marked as green) when exception is thrown.

For

@Test

Junit will consider test as failed if exception is thrown, provided it's an unchecked exception. If the exception is checked it won't compile and you will need to use other methods. This link might help.

are you sure you told it to expect the exception?

for newer junit (>= 4.7), you can use something like (from here)

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


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

and for older junit, this:

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

If your constructor is similar to this one:

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

Then, when you run this JUnit test you will get a green bar:

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

An adventage of use ExpectedException Rule (version 4.7) is that you can test exception message and not only the expected exception.

And using Matchers, you can test the part of message you are interested:

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

Though @Test(expected = MyException.class) and the ExpectedException rule are very good choices, there are some instances where the JUnit3-style exception catching is still the best way to go:

@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());
}

Another library that claims to help here is catch-exception; however, as of May 2014, the project appears to be in maintenance mode (obsoleted by Java 8), and much like Mockito catch-exception can only manipulate non-final methods.