0

I've method like this,

public void beforeTest(String a, String b, String c, String d) {
   x = getURL(a, b, c, d);
   --------
   --------
}

I now want to pass String[] instead of individual Strings as arguments type, so tried this,

public void beforeTest(String[] args){
   x = getURL(a, b, c, d);
   --------
   --------
} 

and passed a,b,c,d as command line arguments, my code isn't working, is it the right approach?

Thank you all, I did try getURL(args[0].......) approach and unfortunately I'm still getting the same error,

[testng] FAILED CONFIGURATION: @BeforeMethod beforeTest [testng] org.testng.TestNGException: [testng] Method beforeTest requires 1 parameters but 4 were supplied in the @Configuration annotation.

By the way, my getURL method is defined as below,

protected String getURL(String a, String b, String c, String d) {
   if (condition){
      return a
   } else if (condition) {
      return b
   } ....
6
  • 2
    you have an args array parameter, but then you don't use it in your call to getURL(). So, what is the point? Don't you want, "x = getURL(args[0], args[1],,, ), etc? Commented Dec 11, 2013 at 22:46
  • 1
    getURL(args[0], args[1], args[2], args[3]); Commented Dec 11, 2013 at 22:46
  • use getURL( args[0], args[1], args[2], args[3] ) but you probably want to do some checking before to make sure you got the right number of arguments. Commented Dec 11, 2013 at 22:46
  • if you use beforeSuite(String ... args) you don't have to touch client code. Commented Dec 11, 2013 at 22:49
  • Check stackoverflow.com/questions/7607353/… Commented Dec 11, 2013 at 22:51

5 Answers 5

2

Try this.

String a = args[0];
String b = args[1];
String c = args[2];
String d = args[3];

Passing of the 4 strings as command line arguments is OK.

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

Comments

1

You might want to use varargs instead. It allows you to treat the input as an array and doesn't require the caller to create an array himself.

public void beforeSuite(String... args){
   x = getURL(args[0], args[1], args[2], args[3]);
} 

You can call this like this:

beforeSuite("test", "test", "test again", "more tests", "even more tests");
// Notice how I can specify more than 4 (or less, or equal to 4) strings
// without changing the method signature

The major issue is accessing your variables though: args[0] instead of args. args is just an array, but args[0] is a value in the array.

Comments

0

I think shortest way to do that is

x = getURL(args);

This will only work if getURL is declared as getURL(String...) or getURL(String[])

Comments

0
public void beforeSuite(String[] args){
   x = getURL(args[0], args[1], args[2], args[3]);
   --------
   --------
}

But why not give getUrl similar type of signature with parameter type String[]. Then you could again pass the args invoking getUrl(args). You can also declare both method with varargs parameter: beforeSuite(String... args) and getURL(String... args)

Comments

0

To do what you need you have to change your method.

public void beforeSuite(String...args){
   x = getURL(args[0], args[1], args[2], args[3]);
   --------
   --------
} 

But keep in mind that this definition (String...args) on a method can have N number of arguments, which means that if you pass less than 4 arguments you will get an ArrayIndexOutOfBoundsException.

Comments

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.