JUnit テストを実行している不安定な ENV があります。
接続/ネットワーク/非テスト関連の問題のため、多くのテストがbefore()
メソッドで失敗し、テストが完全に失敗する前にメソッドの前に再試行する機能を追加したい。
コードスニペットを追加しましたが、これが最善の方法/プラクティスであるかどうかはわかりません...
//hold the max number of before attempts
final private int retries = 3;
//will count number of retries accrued
private int retrieCounter = 0 ;
@Before
public void before() {
try {
//doing stuff that may fail due to network / other issues that are not relevant to the test
setup = new Setup(commonSetup);
server = setup.getServer();
agents = setup.getAgents();
api = server.getApi();
setup.reset();
}
//if before fails in any reason
catch (Exception e){
//update the retire counter
retrieCounter++;
//if we max out the number of retries exit with a runtime exception
if (retrieCounter > retries)
throw new RuntimeException("not working and the test will stop!");
//if not run the before again
else this.before();
}
}