Java JUnit @Rule 将参数传递给测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3060631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:06:49  来源:igfitidea点击:

JUnit @Rule to pass parameter to test

javaparametersjunitjunit-rule

提问by IAdapter

I'd like to create @Ruleto be able to do something like this

我想创建@Rule能够做这样的事情

@Test public void testValidationDefault(int i) throws Throwable {..}

Where i is parameter passed to the test by @Rule.

其中 i 是传递给测试的参数@Rule

However I do get

但是我确实得到

java.lang.Exception: Method testValidationDefault should have no parameters

is there any way to bypass it and set the i parameter in the @Rule?

有没有办法绕过它并在中设置 i 参数@Rule

采纳答案by IAdapter

it can't be done, you can't pass parameters to test method even using @Rule.

它无法完成,即使使用@Rule,您也无法将参数传递给测试方法。

回答by MarcoS

I use @Parametersand @RunWith(value = Parameterized.class)for passing values to tests. An example can be found here.

我使用@Parameters@RunWith(value = Parameterized.class)将值传递给测试。一个例子可以在这里找到。

I did not know about the @Ruleannotation, but after reading this post, I think it serves another purpose than passing parameters to the tests:

我不知道@Rule注释,但在阅读了这篇文章后,我认为它除了将参数传递给测试还有另一个目的:

If in your test class, you create a field pointing to an object implementing the MethodRule interface, and you mark this to be processed as a rule, by adding the @Rule implementation, then JUnit will call back on your instance for every test it will run, allowing you to add additional behavior around your test execution.

如果在您的测试类中,您创建了一个指向实现 MethodRule 接口的对象的字段,并通过添加 @Rule 实现将其标记为规则进行处理,那么 JUnit 将在每次测试时回调您的实例运行,允许您围绕测试执行添加其他行为。

I hope this helps.

我希望这有帮助。

回答by David H. Clements

It should be noted that it is no longer true that you can't pass parameters directly to a test method. It can now be done using Theoriesand @DataPoints/@DataPoint.

应该注意的是,不能将参数直接传递给测试方法已不再正确。现在可以使用Theories@DataPoints/来完成@DataPoint

For example:

例如:

@RunWith(Theories.class)
public class TestDataPoints {

    @DataPoints
    public static int [] data() {
        return new int [] {2, 3, 5, 7};
    }

    public int add(int a, int b) {
        return a + b;
    }

    @Theory
    public void testTheory(int a, int b) {
        System.out.println(String.format("a=%d, b=%d", a, b));
        assertEquals(a+b, add(a, b));
    }
}

Output:

输出:

a=2, b=2
a=2, b=3
a=2, b=5
a=2, b=7
a=3, b=2
a=3, b=3
a=3, b=5
a=3, b=7
a=5, b=2
a=5, b=3
a=5, b=5
a=5, b=7
a=7, b=2
a=7, b=3
a=7, b=5
a=7, b=7

With the test passing.

随着测试通过。

回答by Jens Schauder

As IAdapter said you can't pass an argument using Rules, but you can do something similar.

正如 IAdapter 所说,您不能使用规则传递参数,但您可以执行类似的操作。

Implement a Rule that holds all your parameter values and evaluates the test once for every parameter value and offers the values through a method, so the test can pull them from the rule.

实现一个包含所有参数值的规则,并对每个参数值评估一次测试,并通过方法提供这些值,以便测试可以从规则中提取它们。

Consider a Rule like this (pseudo code):

考虑这样的规则(伪代码):

public class ParameterRule implements MethodRule{
    private int parameterIndex = 0;
    private List<String> parameters;
    public ParameterRule(List<String> someParameters){ 
        parameters = someParameters;
    }

    public String getParameter(){
        return parameters.get(parameterIndex);
    }

    public Statement apply(Statement st, ...){
        return new Statement{
             public void evaluate(){
                 for (int i = 0; i < parameters.size(); i++){
                     int parameterIndex = i;
                     st.evaluate()
                 }      
             }
        }
    }
}

You should be able to use this in a Test like this:

您应该能够在这样的测试中使用它:

 public classs SomeTest{
     @Rule ParameterRule rule = new ParameterRule(ArrayList<String>("a","b","c"));

     public void someTest(){
         String s = rule.getParameter()

         // do some test based on s
     }
 }

回答by piotrek

recently i started zohhakproject. it lets you write tests with parameters (but it's a runner, not a rule):

最近我开始了zohhak项目。它允许您使用参数编写测试(但它是一个运行程序,而不是规则):

@TestWith({
   "25 USD, 7",
   "38 GBP, 2",
   "null,   0"
})
public void testMethod(Money money, int anotherParameter) {
   ...
}