Home > other >  Is it possible to access other class method without passing method parameter
Is it possible to access other class method without passing method parameter

Time:08-07

I have scenario where i want to access another class method which is returning something.

The method in another class is expecting parameter

Example:

public class Class1()
{
    public Response postResponse(String getURL,DataTable dataTable)
    {
        /*..
        my post request code here
        ..*/
        return postData;
    }
}

public class Class2()
{
    public void readPostResponse()
    {
        /*..
         here i want to access Class1.postResponse method and I don't want to pass the 
         parameter.. 
        ..*/
    }
}

Please let me know how to achieve this.

CodePudding user response:

You have three choices:

  • pass parameters. This is preferred way. Because parameters of method contains some info that can be used in method
  • mock or stub your parameters if it is possible. It looks like you are using Selenium, but Selenium starts a real browser
  • pass null parameters.It is not really preferred way because your method can work incorrectly without necessary data

CodePudding user response:

I started to write this before @StepUp posted his answer. I am going to cover the way I think it should be done based on what I understood by "I don't want to pass the parameter"

It sounds you want to create a default case. This example is arbitrary since your code is not really specific. In general terms, when talking about functions, a default case for a function is a method with the same name, with no parameter list. For example,

public class Class1 
{
    /**
     * Default case
     */
    public Response postResponse() 
    {
        // Some code here
    }

    /**
     * Specific case (the ellipsis means some parameter list) 
     */
    public Response postResponse(...)
    {
        // Some code here
    }
}

In your case, you may want to create a default step definition that you can invoke independently in a specific scenario, or you may want to call it from the same scenario with or without parameters specified in your data table. You can still take the same approach. I want to mention that having a default case, doesn't mean that you absolutely need to pass null parameters. 99% of the time, this is a bad idea. All you need to do is to pass some default values.

public class Class1 
{
    /**
     * Default case
     */
    public Response postResponse() 
    {
        String url = "..."; // maybe a base URL here?
        Object col1 = ...;
        Object col2 = ...;
        handlePostResponse(url, col1, col2);
        // Do other stuff?
        return response;
    }

    /**
     * Specific case 
     */
    public Response postResponse(String url, DataTable dataTable)
    {
        // Iterate through data table and call handlePostResponse for each row
        return response;
    }


    // This method wraps how to handle post response. The ellipsis might be the elements of the data table
    private void handlePostResponse(String url, Object col1, Object col2) 
    {
        // Do something
    }
}

Now that you have this, you can call your method in Class 2 that needs to invoke the default case in Class 1.

public class Class2 
{
    public void readPostResponse()
    {
        Class1 clazz1 = new Class1();
        Response resp = clazz1.postResponse(); // calling no parameters (default) case (you may not need to get the response object (ignore it altogether)
        // Do more stuff?
    }
}

Remember that every time you pass null to a function or return null from a function, you may have to do some sort of input or output validation to ensure the object about to be used to invoke other methods is not null. Not doing so will result in Null Pointer Exceptions.

One last note: I am using Object merely for illustration. In your case, it will be whatever type of object each column represent. Most likely, it will be String, but it could also be a number wrapper (i.e. Integer) or some custom data type you might've created for your scenario(s).

  • Related