Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

java - Can a JUnit testmethod have a argument?

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

}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Daniel Mayer's blog has an example of this.

Another, more simple example is on mkyong's webpage


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...