可以在测试行为中创建复杂的代码部分,以在某些情况下强制得出“失败”或“错误”测试结论。例如,如果程序进入不应该执行的特定代码部分时,您可能想要产生“失败”状态。
public void testAddLocation() { LocationService locationService = new LocationService(); Location location = new Location(); location.setAddressLine1("841 Cameron Street"); location.setCity("New York"); location.setCountryCode("USA"); try { locationService.add(location); } catch (Throwable t) { ComponentTest.fail("Add location failed"); } } public void testConnectDB() { DB dataBase = new DB(); try { dataBase.connect(); } catch (ConnectionFailedException e) { ComponentTest.error("Unable to connect to database"); } }