JUnit 测试预期异常的正确方法
JUnit right way of test expected exceptions
大家好,我想知道这种测试异常的方法是否可行,我有这个异常,我需要放入第二个测试注释,结果我收到一个红色的邪恶条,一个成功和一个失败,你可以猜到失败是我关心的,我有一个 fail();在那里,但原因是因为我读到那是测试异常的方法,现在我很困惑。
另外我不得不说我会得到绿色条,因为我期待异常,但我不知道失败是否是查看预期异常答案的正确方法。
此外,如果您有任何建议,我将不胜感激
@Before
public void setUp() throws Exception {
LogPack.logPacConfig(Constants.LOGGING_FILE);
gtfri = "+RESP:GTFRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
weirdProtocol = "+RESP:GRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
factory = new LocomotiveFactory();
}
@Test
public void GTFRICreationTester_shouldPass() throws TramaConProtolocoloDesconocido {
assertTrue(factory.createLocomotive(gtfri, false, new Date()) instanceof LocomotiveGTFRI);
}
@Test(expected = TramaConProtolocoloDesconocido.class)
public void GTFRICreationTester_shouldFail() {
try {
factory.createLocomotive(weirdProtocol, false, new Date());
fail("Expected an TramaConProtolocoloDesconocido");
} catch (TramaConProtolocoloDesconocido e) {
//assertSame("exception thrown as expected", "no se conoce el protocolo dado para la creacion de este factory", e.getMessage());;
}
}
您不需要用 try-catch
赶上 Exception
@Test(expected = TramaConProtolocoloDesconocido.class)
public void GTFRICreationTester_shouldFail() {
factory.createLocomotive(weirdProtocol, false, new Date());
}
如果我们假设当您应用使 exception
抛出的场景时 factory.createLocomotive(weirdProtocol, false, new Date())
抛出 exception
。
void createLocomotive(param...) {
//something...
throw new TramaConProtolocoloDesconocido();
}
您可以使用ExpectedException,它可以为您提供有关预期抛出的异常的更准确信息,并能够验证错误消息,如下所示:
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
public class TestClass {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void GTFRICreationTester_shouldFail() {
expectedException.expect(TramaConProtolocoloDesconocido.class);
factory.createLocomotive(weirdProtocol, false, new Date());
}
}
想了解更多,可以参考我写的博客- Expected Exception Rule and Mocking Static Methods – JUnit
如果您使用的是 java 8,我建议您使用 AssertJ 库
public void GTFRICreationTester_shouldFail() {
assertThatExceptionOfType(EXCEPTION_CLASS).isThrownBy(() -> { factory.createLocomotive(weirdProtocol, false, new Date()) })
.withMessage("MESSAGE")
.withMessageContaining("MESSAGE_CONTAINING")
.withNoCause();
}
使用该解决方案,您可以同时验证异常类型、消息等。
如需更多阅读,请查看:
http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#exception-assertion
有 3 种最常用的方法来测试预期异常:
第一种是最常用的方法,但你只能用它来测试预期异常的类型。如果不抛出 ExceptionType
,此测试将失败:
@Test(expected = ExceptionType.class)
public void testSomething(){
sut.doSomething();
}
此外,您不能使用此方法指定失败消息
更好的选择是使用 ExpectedException JUnit @Rule。在这里你可以为预期的异常断言更多
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testSomething(){
thrown.expect(ExceptionType.class);
thrown.expectMessage("Error message");
thrown.expectCause(is(new CauseOfExeption()));
thrown.reportMissingExceptionWithMessage("Exception expected");
//any other expectations
sut.doSomething();
}
第三个选项将允许您执行与使用 ExpectedException @Rule 相同的操作,但所有断言都应手动编写。然而,这种方法的优点是您可以使用任何自定义断言和任何您想要的断言库:
@Test
public void testSomething(){
try{
sut.doSomething();
fail("Expected exception");
} catch(ExceptionType e) {
//assert ExceptionType e
}
}
大家好,我想知道这种测试异常的方法是否可行,我有这个异常,我需要放入第二个测试注释,结果我收到一个红色的邪恶条,一个成功和一个失败,你可以猜到失败是我关心的,我有一个 fail();在那里,但原因是因为我读到那是测试异常的方法,现在我很困惑。
另外我不得不说我会得到绿色条,因为我期待异常,但我不知道失败是否是查看预期异常答案的正确方法。
此外,如果您有任何建议,我将不胜感激
@Before
public void setUp() throws Exception {
LogPack.logPacConfig(Constants.LOGGING_FILE);
gtfri = "+RESP:GTFRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
weirdProtocol = "+RESP:GRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
factory = new LocomotiveFactory();
}
@Test
public void GTFRICreationTester_shouldPass() throws TramaConProtolocoloDesconocido {
assertTrue(factory.createLocomotive(gtfri, false, new Date()) instanceof LocomotiveGTFRI);
}
@Test(expected = TramaConProtolocoloDesconocido.class)
public void GTFRICreationTester_shouldFail() {
try {
factory.createLocomotive(weirdProtocol, false, new Date());
fail("Expected an TramaConProtolocoloDesconocido");
} catch (TramaConProtolocoloDesconocido e) {
//assertSame("exception thrown as expected", "no se conoce el protocolo dado para la creacion de este factory", e.getMessage());;
}
}
您不需要用 try-catch
Exception
@Test(expected = TramaConProtolocoloDesconocido.class)
public void GTFRICreationTester_shouldFail() {
factory.createLocomotive(weirdProtocol, false, new Date());
}
如果我们假设当您应用使 exception
抛出的场景时 factory.createLocomotive(weirdProtocol, false, new Date())
抛出 exception
。
void createLocomotive(param...) {
//something...
throw new TramaConProtolocoloDesconocido();
}
您可以使用ExpectedException,它可以为您提供有关预期抛出的异常的更准确信息,并能够验证错误消息,如下所示:
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
public class TestClass {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void GTFRICreationTester_shouldFail() {
expectedException.expect(TramaConProtolocoloDesconocido.class);
factory.createLocomotive(weirdProtocol, false, new Date());
}
}
想了解更多,可以参考我写的博客- Expected Exception Rule and Mocking Static Methods – JUnit
如果您使用的是 java 8,我建议您使用 AssertJ 库
public void GTFRICreationTester_shouldFail() {
assertThatExceptionOfType(EXCEPTION_CLASS).isThrownBy(() -> { factory.createLocomotive(weirdProtocol, false, new Date()) })
.withMessage("MESSAGE")
.withMessageContaining("MESSAGE_CONTAINING")
.withNoCause();
}
使用该解决方案,您可以同时验证异常类型、消息等。
如需更多阅读,请查看: http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#exception-assertion
有 3 种最常用的方法来测试预期异常:
第一种是最常用的方法,但你只能用它来测试预期异常的类型。如果不抛出 ExceptionType
,此测试将失败:
@Test(expected = ExceptionType.class)
public void testSomething(){
sut.doSomething();
}
此外,您不能使用此方法指定失败消息
更好的选择是使用 ExpectedException JUnit @Rule。在这里你可以为预期的异常断言更多
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testSomething(){
thrown.expect(ExceptionType.class);
thrown.expectMessage("Error message");
thrown.expectCause(is(new CauseOfExeption()));
thrown.reportMissingExceptionWithMessage("Exception expected");
//any other expectations
sut.doSomething();
}
第三个选项将允许您执行与使用 ExpectedException @Rule 相同的操作,但所有断言都应手动编写。然而,这种方法的优点是您可以使用任何自定义断言和任何您想要的断言库:
@Test
public void testSomething(){
try{
sut.doSomething();
fail("Expected exception");
} catch(ExceptionType e) {
//assert ExceptionType e
}
}