1

I have a program, containing 3 parameterized methods, that receives arguments from the command line. I would like to test them and I have written a test in JUnit, by right clicking on the original class and clicking new Junit test. In the test class, I have annotated the class, as @RunWith(Parameterized.class), and the parameterized methods, as @Parameters and the testmain method as @test.

In each method, I have created a reference for the original class and I have called the methods and passed the required parameters. Now the there is an Initialization error, that says there are no public static methods in the test class. Can someone advise me if this is the right way to carry out a test and if not, what is the right way to do it.

Just to make myself clear, I shall also give an example of what I have done so far (this is not the original code.)

@RunWith(Parameterized.class)
Public class customertest(){

@Parameters
public testmethod1(String a, String b){
customer test = new customer();
test.method1(a, b);
}

@Parameters
public testmethod2(String c, String d){
customer test = new customer();
test.method2(c, d);
}

@parameters
public testmethod3(String e){
customer test = new customer();
test.method3(e);

}

@Test
public static void testmain(String [] args){
customertest tester = new customertest();
tester.testmethod1(args[0], args[1]);
tester.testmethod2(args[2], args[3]);
tester.testmethod3(args[4]);


}




}
7
  • customertest is class that has unit tests in it? Commented Apr 28, 2013 at 14:09
  • yes and customer is the class I am testing. Commented Apr 28, 2013 at 14:12
  • so then you should definitely create instance of the tested class, not of class that obtains tests Commented Apr 28, 2013 at 14:25
  • yes thats what I did I specified wrong in the code am very sorry. Commented Apr 28, 2013 at 14:28
  • I have edited now. Sorry for my blunder. Commented Apr 28, 2013 at 14:30

1 Answer 1

2

You are using @Parameters completely wrong (in fact what you have in not valid Java syntax as the methods testmethodsx do not have return values).

See the example at this site.

There should be only one @Parameters method. It should be static and return a Collection<Object[]>. The number of elements in the array must be equal to the number of arguments in the test class' constructor.

In the example you will see that FibonacciTest has a constructor that takes 2 arguments. Each array returned by the @Parameteres method contains 2 elements. These elements are passed to the constructor and the tests should use the fields to make parameter-specific tests.

Sign up to request clarification or add additional context in comments.

4 Comments

I have gone through this example and other examples too. But all of them, enter values for the test in the @Parameters method. I would like to enter values from the command line. Is that possible??
Although there are ways to do this, I don't think that it is appropriate for JUnit. You could do it via system variables but if you are doing this to drive tests, this is a bad design.
Could you tell me a good means of running tests with command line arguments, on Eclipse. I use Eclipse Juno. Sorry for asking too many questions. I am new to Java. Thanks.
You can use environment variables as in this link: docs.oracle.com/javase/tutorial/essential/environment/env.html. You can google how to set an En Var in Eclipse. I will not go any further since this is REALLY not a good way to do this. This should be done with a properties file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.