JUnit 5能够保证有序测试方法之间的happens-before关系吗?

huangapple go评论64阅读模式
英文:

Does JUnit 5 guarantee the happens-before relationship for ordered test methods?

问题

有时我会使用以下注解来排序测试方法:@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

在这种情况下,JUnit 5是否保证这些方法之间的happens-before关系?换句话说,一个测试方法所做的所有更改是否对所有后续的测试方法可见?

以下是我的具体用例,尽管我不确定它是否被视为良好的风格:

我有一个集成测试,执行以下几个步骤:1)连接到服务器(即创建连接对象),2)发出request1,3)发出request2。这些请求的顺序很重要。我将这些步骤拆分为多个按顺序执行的JUnit测试方法。步骤1)将连接对象存储在静态字段中,以便步骤2)和3)可以访问它。我想知道我是否必须将步骤1)创建的连接对象设置为volatile,或者JUnit是否保证它在步骤2)和3)中的可见性。

英文:

I sometimes order test methods using the following annotation: @TestMethodOrder(MethodOrderer.OrderAnnotation.class)

Does JUnit 5 guarantee the happens-before relationship for these? In other words are all changes by a test method visible to all subsequent test method?

Here is my concrete use case though I'm not sure if its considered good style:

I have an integration test that executes several steps: 1) connect to a server (i.e. create a connection object), 2) issue request1, 3) issue request2. The order of these requests is important. I split these steps into several JUnit test methods executed in order. Step 1) stores the connection object in a static field so it is accessible by steps 2) and 3). I was wondering if I have to make the connection object created by step 1) volatile or if JUnit guarantees its visibility in steps 2) and 3).

答案1

得分: 0

是的,JUnit 5在其默认配置中保证happens-before关系,因为默认情况下,类的所有测试都由同一线程按顺序执行,如此文档1所述。

英文:

Yes, JUnit 5 guarantees the happens-before-relationship in its default configuration because by default all tests of a class are executed sequentially by the same thread as documented here.

huangapple
  • 本文由 发表于 2020年10月7日 18:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定