English 中文(简体)
日美特@
原标题:JUnit @Rule to pass parameter to test

我愿创建@Rule,以便能够做与这一条类似的事情。

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

如果一对准参数通过 rel=“noreferer”>@Rule

然而,我确实是这样。

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

http://blog.mycila.com/2009/11/form-your-own-junit-extensions-using.html“rel=“noreferer”>@Rule?

问题回答

正如艾得伯尔说过,你可以采用《规则》提出论点,但你可以做一些类似的事情。

实行一条规则,规定你的参数价值,一度评估每一参数的价值,通过某种方法提供数值,从而检验标准能够使其偏离规则。

考虑这样的规则(可行的法典):

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()
                 }      
             }
        }
    }
}

你们应当能够在这样的试验中使用:

 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
     }
 }

最近,i开始zohhak。 它允许你用参数书写测试(但是一种操作器,而不是一条规则):

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

应当指出的是,你不能将参数直接通过试验方法。 http://kentbeck.github.com/junit/javadoc/latest/org/junit/experimental/theories/Theories.html

例如:

@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));
    }
}

产出:

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

试验经过了。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签