JUnit + Java + ErrorCollector问题

时间:2016-05-13 10:17:38

标签: java junit junit-rule

我在使用Java中的ErrorCollectors时遇到了一些麻烦。

我有一些代码,比较两个值。如果值匹配,则结果为pass。如果值不匹配,则失败。听起来很简单。所以我创建了一个基本的测试用例:

public class CB_Test {


    @Rule
    public ErrorCollector collector = new ErrorCollector();

    @Before
    public void setUp() {
        //steps = new WebDriverSteps(new FirefoxDriver());
        //steps = new WebDriverSteps();
    }

    @Test
    public void testme() {
        String checkMe;
        String value;

        checkMe = "1234";
        value   = "2234";

        System.out.println("value coming in : " + value);
        System.out.println("value to check  : " + checkMe);
        collector.checkThat("Check values match", value, is(checkMe));

    }
}

其行为完全符合我的要求。但是我希望能够从其他地方调用此代码。所以我创建了这样的“主”文件:

public class ABC_Test {

    @Before
    public void setUp() {
        //steps = new WebDriverSteps(new FirefoxDriver());
        //steps = new WebDriverSteps();
    }

    @Test
    public void check() {
        CheckVal dv = new CheckVal();

        try {
            dv.checkTable("4234");
        } catch (AssertionError er) {
            System.out.println("22");
        } catch (Exception e) {
            System.out.println("23");
        } catch (Throwable t) {
            System.out.println("24");
        }

    }

}

移动代码进行检查:

public class CheckVal {

    @Rule
    public ErrorCollector collector = new ErrorCollector();

    public void checkTable(String value) {

        String checkMe;

        checkMe = "1234";

        System.out.println("value coming in : " + value);
        System.out.println("value to check  : " + checkMe);
        collector.checkThat("Check values match", value, is(checkMe));

    }

}

但是现在当我运行代码时,我总是得到一个传递,即使我引入一个值导致无法生成。我在这里看不出我做错了什么。 (我知道代码很乱 - 这只是我试图将事情分解为最简单的方式来尝试查看我的问题。)

1 个答案:

答案 0 :(得分:2)

@Rule未包含在测试类中,由测试运行器运行并处理所有注释。您的代码,此时只是代码。

您需要更改代码以将@Rule移动到测试中的类,以便处理注释:

import org.junit.*;
import org.junit.rules.ErrorCollector;

public class ABC_Test {
    @Rule
    public ErrorCollector collector = new ErrorCollector();

    @Before
    public void setUp() {
       //steps = new WebDriverSteps(new FirefoxDriver());
       //steps = new WebDriverSteps();
    }

   @Test
   public void check() {
      CheckVal dv = new CheckVal(collector);

      try {
          dv.checkTable("4234");
      } catch (AssertionError er) {
        System.out.println("22");
      } catch (Exception e) {
        System.out.println("23");
      } catch (Throwable t) {
        System.out.println("24");
      }
  }

}

然后修改可重复使用的类以接受ErrorCollector并正常处理:

import org.junit.*;
import org.junit.rules.ErrorCollector;
import org.hamcrest.CoreMatchers;

public class CheckVal {

public ErrorCollector collector = null;

public CheckVal(ErrorCollector collector) {
    this.collector = collector;
}

public void checkTable(String value) {

    String checkMe;

    checkMe = "1234";

    System.out.println("value coming in : " + value);
    System.out.println("value to check  : " + checkMe);
    collector.checkThat("Check values match", value, CoreMatchers.is(checkMe));

}

}

IntelliJ(或您的测试运行器)然后按预期报告错误:

java.lang.AssertionError: Check values match
Expected: is "1234"
    but: was "4234"