JUnit测试预期异常的正确方法 [英] JUnit right way of test expected exceptions
问题描述
大家好我想知道这种测试我的异常的方式是否正常,我有这个例外我需要抛出第二个测试注释,我接收结果是一个红色的邪恶吧,一个成功和失败,就像你可以猜到失败是我关心的,我有失败();但原因是因为我读了测试异常的方法,现在我很困惑。
Hello guys I was wondering if this way of testing my exception is ok, i have this exception i need to throw in the second test annotation, im receiving as result a red evil bar, and a succeed and a failure, as you can guess the failure is my concern, i have a fail(); there but the reason is because i read thats the way to test the exception and now im confused.
此外,我必须说im willin得到绿色条,因为我期待着异常,但我不知道失败是否是查看预期异常答案的正确方法。
Also i have to say im willin get the green bar because im expecting the exception, but i dont know if failure is the right way to see the answer of the expected exception.
此外,如果您有任何建议,我将不胜感激
Also if you had any advice, I would appreciate it
@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());;
}
}
推荐答案
那里是测试预期异常的3种最常用方法:
There is 3 most common ways to test expected exception:
第一种方法是最常用的方法,但您只能使用它来测试预期异常的类型。如果 ExceptionType
将不会被抛出,则此测试将失败:
First one is the most common way, but you can test only the type of expected exception with it. This test will fail if ExceptionType
won't be thrown:
@Test(expected = ExceptionType.class)
public void testSomething(){
sut.doSomething();
}
此外,您无法使用此方法指定失败消息
Also you cannot specify the failure message using this approach
更好的选择是使用 ExpectedException JUnit @Rule 。在这里你可以为预期的异常断言更多
The better option is to use ExpectedException JUnit @Rule. Here you can assert much more for expected exception
@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一样,但所有断言都应手动编写。但是,此方法的优点是您可以使用任何自定义断言和任何所需的断言库:
The third option will allow you to do the same as with using ExpectedException @Rule, but all the assertion should be written manually. However the advantage of this method is that you can use any custom assertion and any assertion library that you want:
@Test
public void testSomething(){
try{
sut.doSomething();
fail("Expected exception");
} catch(ExceptionType e) {
//assert ExceptionType e
}
}
这篇关于JUnit测试预期异常的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!