Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a JUnit testmethod have a argument?

Tags:

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

}

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?

like image 738
sheetal Avatar asked Aug 29 '11 18:08

sheetal


People also ask

Can JUnit test method have parameters?

JUnit allows you to use parameters in a tests class. This class can contain multipletest methods and each method is executed with the different parameters provided. It helps developers save time when executing the same tests which differ only in their inputs and expected results.

Is it possible to pass command line arguments to a test execution in JUnit?

Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below.


4 Answers

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

like image 191
luketorjussen Avatar answered Sep 25 '22 08:09

luketorjussen


Yes you can with the Theories Runner in JUnit 4.4

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

For more details:

  • Junit 4.4 Release Notes
  • Blog
like image 35
Ralph Avatar answered Sep 26 '22 08:09

Ralph


yes, it can. recently i started zohhak project. it lets you write:

@TestWith({
   "25 USD",
   "38 GBP",
   "null"
})
public void testMethod(Money money) {
   ...
}
like image 42
piotrek Avatar answered Sep 24 '22 08:09

piotrek


You can't directly pass parameters to test methods with JUnit. TestNG allows it, though:

//This method will provide data to any test method that declares that its Data
// Provider is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37)},
 };
}

//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
 System.out.println(n1 + " " + n2);
}

will print:

Cedric 36
Anne 37
like image 31
Cedric Beust Avatar answered Sep 25 '22 08:09

Cedric Beust