gpt4 book ai didi

java - JUnit 中的多个规则

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:40:02 25 4
gpt4 key购买 nike

我已经在其中一个测试类中定义了两个规则,但奇怪的是一次只有其中一个起作用——最后定义的那个。

@Rule public ExpectedException exception = ExpectedException.none();    
@Rule public TemporaryFolder folder= new TemporaryFolder();

我一辈子都想不通如何定义两个或多个规则并分别使用它们

最佳答案

我有同样的问题,我发现在那种情况下你可以使用 RuleChain,就像这样:

public TemporaryFolder temp;
public ExpectedException thrown;

@Rule
public TestRule chain =
RuleChain.outerRule(temp = new TemporaryFolder())
.around(thrown = ExpectedException.none());

另一个例子你可以看here , 和 RuleChain javadoc也可以提供帮助。

关于java - JUnit 中的多个规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24982533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com