java 在 jUnit 测试中将参数传递给 @Before 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26357751/
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-11-02 09:47:32  来源:igfitidea点击:

Passing parameter to @Before setup in jUnit test

javaunit-testingjunit

提问by Neil

Is there any way to avoid calling populateRandomData() method at the begining of each test without having a fixed parameter 100. I need to call the same method to setup data before execution of each test but I need to change the number of test data entries e.g. 100 in each case .

有没有办法避免在每个测试开始时调用 populateRandomData() 方法而没有固定参数 100。我需要在执行每个测试之前调用相同的方法来设置数据,但我需要更改测试数据条目的数量例如,每种情况下为 100。

public class Tester
{

   @Before
    public void setUp() {
        populateRandomData(100)
    }

    @Test
    public void testMethod() {

    }

    private void populateRandomData(n){
        //n times insert random data in table.
    }
}

回答by OO7

You can create Parameterized JUnit Testwhich allows you to add number of parameters you want to pass in unit test case. Have a look at example tutorial Create Parameterized Test Case.

您可以创建参数化 JUnit 测试,它允许您添加要在单元测试用例中传递的参数数量。查看示例教程Create Parameterized Test Case

OR

或者

@Rule, using this annotations on your test methods to parameterize the execution of your rules makes it even more useful. Taken from JUnit 4.7 @Rules

@Rule,在您的测试方法上使用此注释来参数化规则的执行使其更加有用。取自JUnit 4.7 @Rules

EDIT :

编辑 :

Example of Using @Rule :

使用@Rule 的示例:

Below is the class which allows you to initialize different value of num variable which will be used in test method:

以下是允许您初始化将在测试方法中使用的 num 变量的不同值的类:

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class Test1 implements TestRule {

    private final int   num;

    public Test1(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public class Test1Statement extends Statement {
        private final Statement statement;

        public Test1Statement(Statement statement, int num) {
            this.statement = statement;
        }

        @Override
        public void evaluate() throws Throwable {
            statement.evaluate();
        }
    }

    @Override
    public Statement apply(Statement statement, Description description) {
        return new Test1Statement(statement, num);
    }
}

The class below is the actual test case class. It contains JUnit test cases & set value of num variable in test method.

下面的类是实际的测试用例类。它包含JUnit 测试用例和测试方法中 num 变量的设置值。

import org.junit.Rule;
import org.junit.Test;

public class RuleNumberTester {

    @Rule
    public Test1    test    = null;

    @Rule
    public Test1    test1   = null;

    @Test
    public void num1Test() {
        test = new Test1(111);
        System.out.println("Num 1 : " + test.getNum());
    }

    @Test
    public void num2Test() {
        test1 = new Test1(222);
        System.out.println("Num 2 : " + test1.getNum());
    }
}

Output :

输出 :

Test cases are executed successfully & shows the values of num variable which was initialized in test methods on console.

测试用例成功执行并显示在控制台上的测试方法中初始化的 num 变量的值。

Num 1 : 111
Num 2 : 222

回答by Duncan Jones

I suppose you could use a @Ruleto ensure populateRandomData()is called each time with the correct parameters.

我想您可以使用 a@Rule来确保populateRandomData()每次都使用正确的参数调用。

However, this gets ugly quickly since you then need to maintain a list of test method names.

然而,这很快就会变得丑陋,因为您需要维护一个测试方法名称列表。

private static final Map<String, Integer> dataCounts = new HashMap<>();

static {
  // list (or otherwise obtain) counts here
  dataCounts.put("testMethod", 100);
}

@Rule
public TestWatcher watcher = new TestWatcher() {
  @Override
  protected void starting(Description description) {
    Integer count = dataCounts.get(description.getMethodName());
    assertNotNull(count);
    populateRandomData(count.intValue());
  };
};