java - 使用 "real data"设置测试系统

标签 java testing junit junit4

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

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

这意味着如果我想保持 @BeforeClass 逻辑,我必须复制测试。我不能使用 @After 和 @Before,因为它们会在每次测试后发生,而且会产生开销。

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

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

编辑:

这是我的类(class)没有 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

}

最佳答案

这取决于您希望如何设置类(class),但您可以使用 ClassRule为了这。这与 TestRule 的作用相同,但它为每个类(class)运行一次,而不是为每个测试运行一次。这可以与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+ 中可用。

关于java - 使用 "real data"设置测试系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12974308/

相关文章:

java - 如何将逻辑与 Controller 分离并使用注释将其放入某些处理程序?

spring - 使用 Autowiring 的 spring 服务测试自定义验证器

junit - 在 JUnit 中将 "assertTrue"重写为 "assertThat"?

java - 如何将 JavaFX 场景函数包含到 public static void main 中

javamail 邮件发不出去

java - Spring : IdempotentReceiverInterceptor can only be used on MessageHandlers?

unit-testing - 仅在测试环境上运行迁移脚本

ruby-on-rails-3 - 我应该在 View 中测试什么?

java - 基于应用参数 JUnit 验证行为

java - 在 java 中模拟 Cipher 时出错