MyRule1和MyRule2MyRule2 依赖于取决于 MyRule1MyRule1因此,“ before”方法应在MyRule2“ before”方法之前运行。在JUnit 4中,可以通过RuleChain通过这种方式实现:
static MyRule1 myRule1 = new MyRule1();
static MyRule2 myRule2 = new MyRule2(myRule1);
@Rule
TestRule ruleChain = RuleChain.outerRule(myRule1)
.around(myRule2);
Run Code Online (Sandbox Code Playgroud)
在JUnit 5中,我必须以这种方式实现它:
static MyRule1 myRule1 = new MyRule1();
@RegisterExtension
static MyRule2 myRule2 = new MyRule2(myRule1);
Run Code Online (Sandbox Code Playgroud)
与MyRule2:
class MyRule2 implements BeforeAllCallback {
private final MyRule1 myRule1;
public MyRule2(MyRule1 myRule1) {
this.myRule1 = myRule1;
}
@Override
public void beforeAll(ExtensionContext extensionContext) {
this.myRule1.beforeAll();
X x = …Run Code Online (Sandbox Code Playgroud) 我刚刚开始编写一些 junit5 测试和扩展。
我很快就遇到了我认为的问题:我如何告诉 junit5ExtensionB需要ExtensionA在场?
例如,我有一个“基本”扩展ExtensionA,它启动数据库并进行一些初始化,这对于某些测试来说已经足够了。
我还ExtensionB“需要”完成一些工作ExtensionA,主要是从商店获取一些对象,然后解析一些参数。
显然,每当我想要扩展 BI 时,也需要扩展 A 存在。有办法强制吗?我尝试过用@ExtendWith(A.class)类进行注释ExtensionB,但这似乎没有效果。
有办法实现我所需要的吗?
或者我只是以错误的方式使用 junit5,应该只使用一个扩展来为我做所有事情?
我写了JUnit5 Extension.但我找不到如何获得测试结果的方法.
扩展看起来像这样:
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.TestExtensionContext;
public class TestResultExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(TestExtensionContext context) throws Exception {
//How to get test result? SUCCESS/FAILED
}
}
Run Code Online (Sandbox Code Playgroud)
任何提示如何获得测试结果?
我找不到任何资源来解释JUnit Jupiter 扩展模型之间BeforeEachCallback和之间的确切区别BeforeTestExecutionCallback。(我当然也对“After”变体感兴趣)
据我了解,以下时间表描述了正在发生的事情:
BeforeEach- BeforeTestExecution- Actual execution of the test- AfterTestExecution-AfterEach
我想BeforeTestExecution存在这样的情况,因此您可以在所有BeforeEach回调都已处理之后但在实际测试执行之前执行代码。然而,这对我来说仍然不清楚,因为每个人都可以使用BeforeTestExecution代替,BeforeEach并且这些回调的执行顺序又是随机的。
那么,BeforeTestExecution如果您同时在多个扩展中使用此回调,究竟是为了什么以及会发生什么?
需要帮助使用带有 PowerMockito 框架的 JUnit5 模拟静态方法。
Powermock junit5 和 mockito2.x 不工作找不到 RunnerTestSuiteChunker
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.*;
@PrepareForTest(EnvironmentUtils.class)
@RunWith(PowerMockRunner.class)
public class RuleControllerTest {
@Before
public void setup() {
PowerMockito.mockStatic(EnvironmentUtils.class);
}
@Test
public void test_rule_create_rule() throws IOException {
when(EnvironmentUtils.isCF()).thenReturn(true);
}
}
Run Code Online (Sandbox Code Playgroud)
和 pom.xml
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<!-- …Run Code Online (Sandbox Code Playgroud) 假设我开发了一个扩展,它不允许测试方法名称以大写字符开头.
public class DisallowUppercaseLetterAtBeginning implements BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) {
char c = context.getRequiredTestMethod().getName().charAt(0);
if (Character.isUpperCase(c)) {
throw new RuntimeException("test method names should start with lowercase.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想测试我的扩展按预期工作.
@ExtendWith(DisallowUppercaseLetterAtBeginning.class)
class MyTest {
@Test
void validTest() {
}
@Test
void TestShouldNotBeCalled() {
fail("test should have failed before");
}
}
Run Code Online (Sandbox Code Playgroud)
如何编写测试以验证执行第二个方法的尝试是否会抛出带有特定消息的RuntimeException?
用户指南包含以下内容:
通常,扩展仅实例化一次。
不是很清楚什么时候extension可以实例化多次?我支持具有多个扩展的测试套件,每个扩展都将其状态存储在类字段中。一切正常,但我可以依赖于此还是应该重构此代码以使用 ExtensionContext.Store?
JUnit5 手册中的主要WebServerExtension示例不完整,并且没有完全显示如何正确存储配置(例如,enableSecurity、服务器 url)。
该示例忽略或硬编码这些值。手册(第 5.11 节。在扩展中保持状态)暗示应该使用“Store”,但在构造对象时 ExtensionContext 尚不可用——尚不清楚如何处理将此数据作为 ExtensionContext 迁移到 Store在构造函数中尚不可用。
我还不清楚,对于 WebServerExtension 编程示例使用 Store API 是否值得,也许它可以仅使用内部状态(例如 this.serverUrl、this.enableSecurity 等)来工作。
也许商店更适用于不使用这种“编程”风格的扩展,其中可能存在(适当地)自定义扩展的多个实例?换句话说,我从指南中不清楚这是否是受支持的范例?
其他在线 JUnit 5 扩展示例(例如 org.junit.jupiter.engine.extension.TempDirectory)展示了如何利用注释来处理将配置信息传递到 Store,但如果也有像 WebServerExtension 这样的完整编程构建器类型示例,那就太好了。
像 TempDirectory 这样的示例显然可以从 beforeXXX() 方法访问 ExtensionContext,而 WebServerExtension 示例则不能。
使用下面的方法似乎工作正常,但我想确认这是一个受支持的范例(即在使用这种编程方法时使用字段而不是商店)。
public class WebServerExtension implements BeforeAllCallback {
private final boolean securityEnabled;
private final String serverUrl;
public WebServerExtension(Builder builder) {
this.securityEnabled = builder.enableSecurity;
this.serverUrl = build.serverUrl;
}
@Override
public void beforeAll(ExtensionContext context) {
// is it ok to use this.securityEnabled, this.serverUrl instead …Run Code Online (Sandbox Code Playgroud) 我正在尝试为Junit5编写一个类似于Junit4的扩展,但是我没有意识到在新的(无状态)扩展系统中如何做到这一点.
之前版本中的想法是用户可以将信息传递到扩展类,从而改变其行为方式.这是一个伪代码段,大致显示了以前的用法:
public void MyTest {
// here I can define different behaviour for my extension
@Rule MyCustomRule rule = MyCustomRule.of(Foo.class).withPackage(Bar.class.getPackage).alsoUse(Cookies.class);
@Test
public void someTest() {
// some test code already affected by the @Rule
// plus, user has access to that class and can use it, say, retrieve additional information
rule.grabInfoAboutStuff();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道如何操作JUnit 5扩展,使用什么生命周期等等.但我不知道如何让测试编写者有权使用JUnit5修改扩展的行为.任何指针赞赏.
Mockito 不会在 @BeforeAll 注释方法中使用 JUnit 5 初始化模拟运行。如果我将 init 方法注释更改为 @BeforeEach,它会起作用。测试在 IntelliJ IDEA 中运行。
我的测试课:
@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MyTest {
private MyMockedClass myMockedClass;
@BeforeAll
public void init() {
when(myMockedClass.getSomething()).thenReturn(something); // Mock is not initialized, getting NPE on test
Run Code Online (Sandbox Code Playgroud)
依赖关系(仅显示相关的,为简洁起见省略了其他的):
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ XXX ---
[INFO] +- org.mockito:mockito-core:jar:3.6.28:test
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.10.18:compile
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.10.18:test
[INFO] | \- org.objenesis:objenesis:jar:3.1:test
[INFO] +- org.mockito:mockito-junit-jupiter:jar:3.6.28:test
[INFO] | \- org.junit.jupiter:junit-jupiter-api:jar:5.7.0:test
[INFO] | +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-commons:jar:1.7.0:test …Run Code Online (Sandbox Code Playgroud) 我的要求是对一组测试进行一些初始化,并在所有测试完成后将其清除。这些测试围绕几个测试类进行,因为它们不是密切相关的,但需要一个共同的初始化。
我正在使用@SelectClasses来形成套件并尝试使用@ExtendWith应该处理预处理和后处理的自定义扩展。这不起作用,我不确定 JUnit 中是否存在合适的解决方案。共享已经尝试过的示例代码。
套房:
@SelectClasses({FirstTest.class, SecondTest.class})
@ExtendWith(SuiteExtension.class)
@RunWith(JUnitPlatform.class)
@SuiteDisplayName("test suite")
public class SuiteClass {
}
Run Code Online (Sandbox Code Playgroud)
延期:
public class SuiteExtension implements BeforeAllCallback, AfterAllCallback {
@Override
public void afterAll(ExtensionContext context) throws Exception {
System.out.println("afterAll");
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
System.out.println("beforeAll");
}
}
Run Code Online (Sandbox Code Playgroud)
测试类 1:
public class FirstTest {
@Test
void test1(){
System.out.println("test1");
}
}
Run Code Online (Sandbox Code Playgroud)
测试类 2:
public class SecondTest {
@Test
void test2(){
System.out.println("test2");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
test1
test2
Run Code Online (Sandbox Code Playgroud)
预期输出:
beforeAll
test1
test2 …Run Code Online (Sandbox Code Playgroud) 我在每个@Test 上都有带有 allure2 @Tmslink("1234") 注释的测试。所以,我需要获取 @TmsLink 值并在我的测试中使用它。我在扩展中有注释值,但我如何提供它来测试?
public class TmsLink implements BeforeTestExecutionCallback {
private String tmsLink;
@Override
public void beforeTestExecution(ExtensionContext context) {
findAnnotation(context.getElement(), TmsLink.class).ifPresent(link -> this.tmsLink = link.value());
}
public String getTmsLink() {
return tmsLink;
}
}
@ExtendWith(TmsLink .class)
public abstract class Tests {
}
Run Code Online (Sandbox Code Playgroud)
使用 Junit4,它只是:
@Rule
public TmsLink extension = new TmsLink();
extension.getTmsLink();
Run Code Online (Sandbox Code Playgroud)