2

所以现在我正在使用 JUnit 4,在@BeforeClass方法中我设置了重置用户模式或准备示例数据所需的一切。现在,并不是我不喜欢这种方法,而是我发现它非常令人沮丧,原因如下:

  • 我正在使用Parameterized注释来使用不同的输入数据运行相同的测试。参数化不起作用,@BeforeClass因为@BeforeClass使用静态方法。

这意味着如果我想保持@BeforeClass逻辑,我必须复制测试。我不能使用@After,@Before因为这些都会在每次测试之后发生,这将是一个开销。

我在想我可以重构这个单元测试,因为我将编写一个处理测试的抽象类,并为我想尝试的每个组参数编写一个子类,这样我就可以只编写一次测试代码。

我希望您可以从以下起点建议一个更简洁的选项:使用@Parameterized,每个参数组只需要运行一次“数据库”方法。

编辑:

这是我的班级没有 BeforeClass 的一个例子

RunWith(LabelledParameterized.class)
public class TestCreateCampaign extends AbstractTestSubscriberCampaign {

    public TestCreateCampaign(String label, String apiKey, String userKey,
            int customerId) {
        super(label, apiKey, userKey, customerId);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Parameters
    public static Collection<Object[]> generatedData() {
        return DataProvider.generatedCorrectSubscriberData();
    }

    @Test
    public void testCreateEmailCampaignBothTriggered() {

        // TEST

    }

    @Test
    public void testCreateTextCampaignTriggered() {

        // TEST

    }

    @Test
    public void testCreateTextCampaignTest() {

        // TEST

    }

    // Other Tests

}
4

3 回答 3

0

从参数化测试类的构造函数调用 setup 方法怎么样?

编辑:

好的,我不知道有什么会自动执行此操作,但我认为您可以编写代码Rule来执行此操作。您可以Rule从头开始执行 extend ExternalResource。这就是我认为它会做的事情。

  1. 构造函数将采用测试类的一个实例和一个ExternalResource实例。
  2. 在构造函数中,它会找到包含@Test注释的方法列表,并获得计数。它将迭代计数设置为 0。
  3. 在该before方法中,它将增加迭代计数,如果它在增量之后为 1(或之前为 0),它将调用before传递的方法ExternalResource
  4. 在该after方法中,它将检查迭代计数是否等于测试次数,如果是,则调用after传递的方法ExternalResource

您可能需要使用不同的回调类/接口,ExternalResource因为beforeafter方法是protected. 如果你真的想要很酷,你可以在你的规则中定义你自己的BeforeParametersAfterParameter注解,它会在传递的实例中寻找那些方法。

如果您开发了它,请将其发布或提交给 JUnit 以供包含。

这是我想出的,没有我想要的那么好:

@RunWith(Parameterized.class)
public class TestExample {

private interface BeforeAfter {
    void before();

    void after();
}

public static class Resource extends ExternalResource {

    private final int count;
    private final BeforeAfter ba;
    private int iteration = 0;

    Resource(Object instance, BeforeAfter ba) {
        int localCount = 0;
        for (Method method : instance.getClass().getMethods()) {
            if (method.getAnnotation(Test.class) != null) {
                localCount++;
            }
        }
        this.count = localCount;
        this.ba = ba;
    }

    @Override
    protected void before() throws Throwable {
        if (iteration == 0) {
            ba.before();
        }

        iteration++;
    }

    @Override
    protected void after() {
        if (iteration == count) {
            ba.after();

            iteration = 0;
        }
    }
}

@Parameters
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][] { { 3, 0 }, { 4, 1 } });
}

@Rule
public static Resource resource = new Resource(new TestExample(0, 0), new BeforeAfter() {

    @Override
    public void before() {
        System.out.println("setup");
    }

    @Override
    public void after() {
        System.out.println("cleanup");

    }
});

private int fInput;
private int fExpected;

public TestExample(int input, int expected) {

    // System.out.println("Constructor invoked" + fInput);
    fInput = input;
    fExpected = expected;
}

@Test
public void test1() {
    System.out.println("test1 fInput=" + fInput);
}

@Test
public void test2() {
    System.out.println("test2 fInput=" + fInput);
}
}

结果:

setup
test1 fInput=3
test2 fInput=3
cleanup
setup
test1 fInput=4
test2 fInput=4
cleanup
于 2012-10-19T12:41:20.943 回答
0

这取决于您要如何设置课程,但您可以为此使用ClassRule这与TestRule执行相同的工作,但它为每个类而不是每个测试运行一次。这可以与Parameterized和TestRule结合使用,例如:

@RunWith(Parameterized.class)
public class TestCreateCampaign {
  @ClassRule
  public static ExternalResource beforeAfterClass = new ExternalResource() {

    @Override
    protected void before() throws Throwable {
      System.out.println("before each class");
    }

    @Override
    protected void after() {
      System.out.println("after each class");
    }
  };

  @Rule
  public ExternalResource beforeAfter = new ExternalResource() {
    @Override
    protected void before() throws Throwable {
      System.out.println("before each test");
    }

    @Override
    protected void after() {
      System.out.println("after each test");
    }
  };

  @Parameters(name = "{index}: fib({0})={1}")
  public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][] { { 3, 0 }, { 4, 1 } });
  }

  private int fInput;
  private int fExpected;

  public TestCreateCampaign(int input, int expected) {
    fInput = input;
    fExpected = expected;
  }

  @Test
  public void test1() {
    System.out.println("test1 fInput=" + fInput);
  }
}

这会产生以下输出:

before each class
before each test
test1 3
after each test
before each test
test1 4
after each test
after each class

这似乎是您正在寻找的。为了减少重复的数量,您当然可以在单独的 java 类中定义 beforeAfterClass 和 beforeAfter。

这些在 JUnit 4.9+ 中可用。

于 2012-10-19T13:24:34.900 回答
0

请参阅如何使用 Spring Test 在每个案例中加载一次 DBUnit 测试数据,了解每次测试运行仅初始化一次测试数据的方法。

于 2012-10-27T06:57:46.490 回答