user3396543
user3396543

Reputation: 1

Need to pass multiple (20+) parameters in a Java method. Any efficient way of doing this?

I have multiple methods in a Java class where every method has 20+ parameters. I will create an object for this class in another class and call each and every method. Typically I'm using the POM (Page Object Model) in Selenium Java. So in every Page object class, there are multiple(20+) parameters in every method, which I will call in the Test Class.

Page Object Class :

public void enterShipInfo(String IMO,String Vstat,String Vcode,String Vname,
        String Vtype,String Officialno,String Buildyr,String Shipyard,String Hullno,String Layingdate,
        String Launcheddate,String Deliverdate,String Reportinclude,String Portregistry,String VFlag,
        String Vstatus,String Classification,String Classid,String Classnotation,String PI,String HM,
        String Regowner,String Shipmanager,String Comoperator,String Callsign,String SSR,String Factor,
        String ELOG,String Vcomments,String VsisIMO,String Chartertype,String Showonweb){ 

}

.... Other Methods with similar long list of parameters

Then in Test Class, again I'm creating parameters for these:

public class VesTest {

@Test(dataProvider="Ves",priority=1)
public void createVesTest(String IMO,String Vstat,String Vcode,String Vname,
        String Vtype,String Officialno,String Buildyr,String Shipyard,String Hullno,String Layingdate,
        String Launcheddate,String Deliverdate,String Reportinclude,String Portregistry,String VFlag,
        String Vstatus,String Classification,String Classid,String Classnotation,String PI,String HM,
        String Regowner,String Shipmanager,String Comoperator,String Callsign,String SSR,String Factor,
        String ELOG,String Vcomments,String VsisIMO,String Chartertype,String Showonweb 

Mdr_Vessel obj_Mdr_Vessel = page(Mdr_Vessel.class);

  obj_Mdr_Vessel.clickSubmenu();
.....

}

Any efficient way to reduce typing the parameters again in Test Class??? I don't want to break the method into multiple methods. So please suggest me a way of passing parameters in an efficient way

Upvotes: 0

Views: 1769

Answers (5)

Daniel Olszewski
Daniel Olszewski

Reputation: 14411

You should read about the Prameter object patter which deals with that type of problem. In brief, it suggests you to crate a wrapper object for all parameters that the method accepts and use it instead the long list of arguments.

public class YourClassName {
  private String IMO;
  private String Vstat;
  private String Vcode;
  // other parameters

  public YourClassName(String IMO, String Vstat, String Vcode, /* other params*/) {
    this.IMO = IMO;
    this.Vstat = Vstat;
    this.Vcode = Vcode;
    // assign other params
  }

  /getters
}

Upvotes: 1

Blured Derulb
Blured Derulb

Reputation: 241

You can use the builder factory pattern https://jlordiales.wordpress.com/2012/12/13/the-builder-pattern-in-practice/

Upvotes: 0

Nargas
Nargas

Reputation: 121

Can you create a class to regroup all parameters ?

Upvotes: 1

Hoopje
Hoopje

Reputation: 12942

I don't know if this counts as "breaking up the method into multiple methods", but what you can do is collect the parameters in a single object. Then for example

void method(Type1 parameter1, Type2 parameter2, Type3 parameter3) { ... }

becomes:

public class Parameters {
    private Type1 parameter1;
    private Type2 parameter2;
    private Type3 parameter3;

    // getters and setters
}

void method(Parameters params) { ... }

This kind of pattern is often used in a fluent style:

public class Parameters {
    private Type1 parameter1 = /* convenient default value */;
    private Type2 parameter2 = /* convenient default value */;
    private Type3 parameter3 = /* convenient default value */;

    public Parameters setParameter1(Type1 parameter1) {
        this.parameter1 = parameter1;
        return this;
    }

    // other setters in the same style and getters
}

Then you can call your method like:

method(new Parameters().setParameter1(...).setParameter3(...));

(where you only set the parameters with non-default values).

Upvotes: 1

victor gallet
victor gallet

Reputation: 1898

You can create new objects to group your parameters and then use them in your method signature. For example :

public class VParameter {
  String Vstat;
  String Vcode;
  String Vname;
  String Vtyp;

Upvotes: 2

Related Questions