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

时间:2019-07-15 17:26:39

标签: 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才能使其工作吗?

1 个答案:

答案 0 :(得分:0)

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

@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

相关问题