java - 使用JUnit @ResourceLock顺序执行一些测试类

标签 java junit5

我有一个大型集成测试套件,测试类是并行执行的,类方法是顺序执行的。有 2 个测试类使用相同的服务来创建/读取/删除实体,并且共享此服务会创建竞争条件:

  1. Foo 类正在创建一条记录
  2. Class Bar 正在删除所有记录
  3. Foo 类正在尝试读取创建的记录,断言它存在,但此时它已被 Bar 删除。

我尝试使用 JUnit 的 @ResourceLock 注释这两个类,但它不起作用,也许我遗漏了一些东西?

示例:

@ResourceLock("serivceResource")
class Foo {
@Test
void fooTest(){
//create a record
//read the created record
//assert record created
}
}



@ResourceLock("serivceResource")
class Bar {
@Test
void barTest(){
//wipe all records
}
}

Foo#fooTestBar#barTest@ResourceLock 的使用存在竞争条件争议。

我想知道我是否需要以某种方式配置 JUnit 才能使其工作?

最佳答案

我尝试重现并发现下面的测试在有或没有资源锁定的情况下都能按预期工作

@ResourceLock("serivceResource")
class Foo {
    @BeforeAll static void beforeAll() { call("Foo.beforeAll");}
    @BeforeEach void beforeEach() { call("Foo.beforeEach");}
    @AfterAll static void afterAll() { call("Foo.afterAll");}
    @AfterEach void afterEach() { call("Foo.afterEach");}
    @Test void test1(){ call("Foo.test1");}
    @Test void test2(){ call("Foo.test2");}
    @Test void test3(){ call("Foo.test3");}

    private static void call(String action) {
        System.out.println("Enter "+action);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Leave "+action);
    }
}

@ResourceLock("serivceResource")
class Bar {

    @BeforeAll static void beforeAll() { call("Bar.beforeAll");}
    @BeforeEach void beforeEach() { call("Bar.beforeEach");}
    @AfterAll static void afterAll() { call("Bar.afterAll");}
    @AfterEach void afterEach() { call("Bar.afterEach");}
    @Test void test1(){ call("Bar.test1");}
    @Test void test2(){ call("Bar.test2");}
    @Test void test3(){ call("Bar.test3");}

    private static void call(String action) {
        System.out.println("Enter "+action);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Leave "+action);
    }
}

如果设置了资源锁,则输出 - 一切都已序列化

Enter Bar.beforeAll
Leave Bar.beforeAll
Enter Bar.beforeEach
Leave Bar.beforeEach
Enter Bar.test1
Leave Bar.test1
Enter Bar.afterEach
Leave Bar.afterEach
Enter Bar.beforeEach
Leave Bar.beforeEach
Enter Bar.test2
Leave Bar.test2
Enter Bar.afterEach
Leave Bar.afterEach
Enter Bar.beforeEach
Leave Bar.beforeEach
Enter Bar.test3    
Leave Bar.test3
Enter Bar.afterEach
Leave Bar.afterEach
Enter Bar.afterAll
Leave Bar.afterAll
Enter Foo.beforeAll
Leave Foo.beforeAll
Enter Foo.beforeEach
Leave Foo.beforeEach
Enter Foo.test1
Leave Foo.test1
Enter Foo.afterEach
Leave Foo.afterEach
Enter Foo.beforeEach
Leave Foo.beforeEach
Enter Foo.test2
Leave Foo.test2
Enter Foo.afterEach
Leave Foo.afterEach
Enter Foo.beforeEach
Leave Foo.beforeEach
Enter Foo.test3
Leave Foo.test3
Enter Foo.afterEach
Leave Foo.afterEach
Enter Foo.afterAll
Leave Foo.afterAll

如果未设置资源锁,则输出 - 一切都是并行的。 (不过,beforeAll/afterAll 看起来非常非常奇怪!)

Enter Bar.beforeEach
Enter Bar.beforeEach
Enter Bar.beforeEach
Enter Foo.beforeEach
Enter Foo.beforeEach
Enter Foo.beforeEach
Leave Foo.beforeEach
Leave Bar.beforeEach
Leave Foo.beforeEach
Leave Bar.beforeEach
Leave Foo.beforeEach
Leave Bar.beforeEach
Enter Bar.test3
Enter Foo.test1
Enter Bar.test1
Enter Foo.test3
Enter Foo.test2
Enter Bar.test2
Leave Bar.test3
Leave Bar.test1
Leave Foo.test3
Leave Foo.test1
Leave Foo.test2
Leave Bar.test2
Enter Bar.afterEach
Enter Bar.afterEach
Enter Foo.afterEach
Enter Foo.afterEach
Enter Bar.afterEach
Enter Foo.afterEach
Leave Bar.afterEach
Leave Foo.afterEach
Leave Foo.afterEach
Leave Foo.afterEach
Leave Bar.afterEach
Leave Bar.afterEach
Enter Foo.beforeAll
Enter Bar.beforeAll
Leave Foo.beforeAll
Leave Bar.beforeAll
Enter Bar.afterAll
Enter Foo.afterAll
Leave Bar.afterAll
Leave Foo.afterAll

我的猜测 - 您的 Foo 测试会干扰其他一些测试,而不是 Bar

关于java - 使用JUnit @ResourceLock顺序执行一些测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044553/

相关文章:

java - 如何让 App Engine cron 作业显示在 Web 界面的 "scheduled tasks"选项卡中?

java - 如何使用 BST 实现哈希表?

java - JUnit 测试结果在测试失败时显示 0,但在测试正常时显示 1

gradle - 多项目构建:为每个子项目执行功能

java - 在 RecyclerView 中膨胀两种类型的 .XML

java - "Refused to set unsafe header ' Cookie ' "同时以 Angular 6 发送带有 GET 请求的 cookie

spring - 将 Spring bean 注入(inject) JUnit Jupiter 中的 ParameterResolver

java - Junit配置错误: You must provide at least one argument for this @ParameterizedTest

java - 托马克赫斯特线模。尝试连接到 8080。连接被拒绝

java - Log4J 能够从磁盘已满中恢复吗?