JUnit 测试方法可以有参数吗? [英] Can a JUnit testmethod have a argument?

查看:22
本文介绍了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();
    }

}

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

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 测试方法可以有参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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