Currently i am using error collector rule in my application so that though i got any failed assertion in my test it should continue next line of code which is working perfectly.
right now for each failed test i want re rerun test again to confirm is it really failed.
So i amusing retry rule. by using this for each failed assertion it executes whole test again. so if i got 2 failed assertion per test it executes retry code 6 times.
i want retry code should run not per failure but it should executes retry code after whole test execution completes.
package scripts;
import static org.junit.Assert.assertThat;
import java.util.concurrent.Callable;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import scripts.BFM.Retry;
public class BFMNew extends TestBase {
@Rule
public ErrorCollector Errcoll = new ErrorCollector();
@Rule
public Retry retry = new Retry(3);
@Before
public void setUp() throws Exception {
System.out.println(" in before");
}
// ===========Re run fail test custom====
public class Retry implements TestRule {
private int retryCount;
public Retry(int retryCount) {
this.retryCount = retryCount;
}
public Statement apply(Statement base, Description description) {
return statement(base, description);
}
private Statement statement(final Statement base,
final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
// implement retry logic here
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName()
+ ": run " + (i + 1) + " failed");
}
}
System.err.println(description.getDisplayName()
+ ": giving up after " + retryCount + " failures");
throw caughtThrowable;
}
};
}
}
@Test
public void compare() {
int x = 100;
int y= 2;
int z = 10;
System.out.println("“********Actions********”");
try{
//Assert.assertEquals(x,z);
Assert.assertEquals("failed first test",true, false);
}
catch(Throwable t) {
Errcoll.addError(t);
// Write java code to show assertion failed and error caught in try catch
}
System.out.println("Second test to pass testcase With no exception handled");
try{
Assert.assertEquals(x,y);
System.out.println("second test passed");
}
catch(Throwable t) {
System.out.println("second Error caught");
}
System.out.println("after 2nd statement");
System.out.println("third test failed to handle exception");
try{
Assert.assertEquals("failed third test case",true, false);
}
catch(Throwable t) {
Errcoll.addError(t);
}
System.out.println("final statement");
}
@Test
public void testdata() {
System.out.println("testdatapassed");
}
}