JUnit 测试预期异常的正确方法
2022-09-02 03:31:58
大家好,我想知道这种测试我的异常的方式是否正常,我有这个异常,我需要在第二个测试注释中投入,im收到一个红色的邪恶条,以及一个成功和失败,因为你可以猜到失败是我关心的,我有一个失败();但原因是因为我读到这是测试异常的方法,现在我很困惑。
另外,我不得不说,我愿意得到绿色的条,因为我期待异常,但我不知道失败是否是看到预期异常答案的正确方法。
另外,如果您有任何建议,我将不胜感激
@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());;
}
}