JUnit testmethod可以有一个参数吗? [英] Can a JUnit testmethod have a argument?

查看:97
本文介绍了JUnit testmethod可以有一个参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}

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

In the above code I need to pass the array as a argument to a JUnit method, the above code will be present in a JUnit class, can I have these kind of methods in a JUnit class and a test =method with argument?

推荐答案

你应该看看参数化的单元测试(在JUnit 4中引入)。

You should take a look at parameterized unit tests (introduced in JUnit 4).

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

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

Another, more simple example is on mkyong's webpage

这篇关于JUnit testmethod可以有一个参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆