如何在junit中使用重试规则和Errorcollector规则

时间:2013-12-04 11:57:47

标签: junit4

我在我的应用程序中使用错误收集器规则(selenium web driver)。我能够在错误收集器规则的帮助下抛出异常并继续下一行代码。但是现在我想再次运行失败的测试(3次)以确保他们真的失败了。因此我使用重试规则。但是这个规则在单独应用时会被执行(带有Assert命令的重试规则)`但是当用错误收集器写入时不会被执行任何原因.... 请帮我提供示例代码。

TestBase.java:

public class TestBase {

    @Rule
    public ErrorCollector collector = new ErrorCollector();

    private boolean fatal;

    public TestBase() {
          fatal=true;
    }

   public void assertEquals( String msg, Object expected, Object actual) {
          if(getFatal()) {
                Assert.assertEquals(msg,expected, actual);
          } else {
              collector.checkThat(msg, actual, CoreMatchers.is(expected));            
          }
    } 


    public void setFatal(boolean fatalFlag) {
          fatal = fatalFlag;
    }

    public boolean getFatal() {
          return fatal;
    }

}

BFMNew.java

public class BFMNew extends TestBase {

    @Rule
    public Retry retry = new Retry(3);
    @Rule
    public ErrorCollector errocol = new ErrorCollector();

    @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 one() {
         setFatal(false);
         Boolean IsLogin = true;  //Here function will come for login
         Boolean IsPost = null;
         Boolean IsStnComment = null;
         Boolean IsPhotoUpload = false;

         if( IsLogin ) {            
            IsPost = false;         
            assertEquals("Failed to Insert Post", true, IsPost);            
         }

        System.out.println(" After Post ");
        assertEquals("Failed to upload photo", true, IsPhotoUpload);    

        if( IsPost ) {
            IsStnComment = false;
            //assertEquals("Failed to Insert Comment", true, IsStnComment); 
        }

        System.out.println("After comment");

    }

1 个答案:

答案 0 :(得分:0)

问题在于规则排序。您应该使ErrorCollector成为外部规则和Retry内部规则。从junit 4.10开始使用此

class YourTest {

   private ErrorCollector collector = new ErrorCollector();

   private Retry retry = Retry(3);
   @Rule
   public TestRule chain= RuleChain
                      .outerRule(collector)
                      .around(retry);

   // tests using collector go here
}