JUnit testmethod可以有一个参数吗?

she*_*tal 20 java junit

import java.util.regex.Pattern;

public class TestUI {
    private static Pattern p = Pattern.compile("^[A-Za-z0-9()+-]+$");

    public static void main(String[] args) {   
        // Test case1
        String[] str=test();

        System.out.println(str[0]+str.length);
        match("Alphanumeric(Text)");
    }

    private static String[] test() {

        boolean res;
        String[] array={"a","b","c","d","e"};
        for(int i=0;i<array.length;i++){
            System.out.println(match(array[i]));
            res=match(array[i]);
            if(res=true)
                calltomethod(array);
        }

        return array;   
    }

    private static boolean match(String s) {
        return p.matcher(s).matches();
    }

}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我需要将数组作为参数传递给JUnit方法,上面的代码将出现在JUnit类中,我可以在JUnit类中使用这些方法,使用带参数的test =方法吗?

luk*_*sen 7

您应该看看参数化单元测试(在JUnit 4中引入).

Daniel Mayer的博客就是一个例子.

另一个更简单的例子是mkyong的网页


Ral*_*lph 6

是的,您可以使用TheoriesJUnit 4.4中的Runner

@RunWith(Theories.class)
public class TheorieTest {

   @DataPoints
   public static String[] strings={"a","b","c","d","e"};

   private static Pattern p = Pattern.compile("^[A-Za-z0-9()+-]+$");

   @Theory
   public void stringTest(String x) {
      assertTrue("string " + x + " should match but does not", p.matcher(x).matches());

   }
 }
Run Code Online (Sandbox Code Playgroud)

更多细节:


pio*_*rek 5

是的,它可以。最近我开始了zohhak项目。它让你写:

@TestWith({
   "25 USD",
   "38 GBP",
   "null"
})
public void testMethod(Money money) {
   ...
}
Run Code Online (Sandbox Code Playgroud)