1

I have some tests set up that I need to run from Terminal, however I need to be able to choose which URL I want to point to for my API calls instead of live. Is there a way I can do this in terminal?

At the moment I have a string in my Constants.java file that I point to different ones, but I need to do it from terminal apparently! So my string is private static String BASE_URL = "http://www.website.com/" this is the string I want to change in terminal.

I've written the following but it doesn't appear to ask me to input anything.

    @Before
    public void setURLForAPICalls() {
        Scanner url_input = new Scanner(System.in);
        System.out.print("Enter the server you wish to test against: ");
        Constants.BASE_URL = url_input.next();
}

Even if it's a case of setting it up in a @Before test method or something? I've been trying to figure this out for days and starting to think it's impossible to do...

Thanks in advance!

BilalMH
  • 175
  • 1
  • 21
  • What do you mean by _apparently_? It would be helpful if you post your code so we can see what's going on. – px06 Feb 06 '17 at 16:22
  • You can pass in arguments to the main method and parse by those. Relevant question here: http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java – Ceiling Gecko Feb 06 '17 at 16:23
  • @px06 by apparently, I do aha. I have no code to post coz I have no idea how to do it! – BilalMH Feb 06 '17 at 16:24
  • You can edit your post and include code in it. – px06 Feb 06 '17 at 16:25
  • @px06 I've put a bit in, that's what I need to change via terminal but I have no idea how! – BilalMH Feb 06 '17 at 16:26
  • You cannot change the value of `final` members... See this post: http://stackoverflow.com/questions/15655012/how-final-keyword-works – px06 Feb 06 '17 at 16:28
  • You should add some code. Not all of it, just the important part – rafaelc Feb 06 '17 at 16:29
  • @px06 @Rafael Cardoso I've added some code of what I think should work but when I run `./gradlew test it still doesn't ask me to enter a url to use for my tests :( what am I doing wrong? – BilalMH Feb 06 '17 at 16:36

2 Answers2

1

From your question you have stated that in Constants.java you have the following statement:

private static String BASE_URL = "http://www.website.com/";

When you test, you're trying to change this BASE_URL, which you cannot do. Not only is it a private member or Constants but it's also a final member, which means the value of it cannot be changed.

What you can do is create instantiate Constants when your app runs and you can specify it to be either a TEST or a RELEASE version.

Something like the following may be sufficient:

public enum RUN_TYPE {
    TEST, 
    RELEASE;
}

You can modify your Constants class to be something like the following:

public class Constants {

    private static final instance = new Constants();

    private RUN_TYPE type;

    private String BASE_URL;

    // More urls here

    private Constants(){

    }

    public void setRunType(RUN_TYPE type){
        this.type = type;
        if(type == RUN_TYPE.RELEASE){
            BASE_URL = "http://release.api/endpoints";
        } else if(type == RUN_TYPE.TEST){
            BASE_URL = "http://test.api/endpoints";
        }
    }

    public String getBaseUrl(){
        return BASE_URL;
    }

    public static Constants getInstance(){
        return instance;
    }

    // More getters here

}

This is a singleton class that will contain the values needed as you specify.

In release mode you can call:

Constants.getInstance().setRunType(RUN_TYPE.RELEASE);

And while testing you can do something like:

@RunWith(AndroidJUnit4.class)
public class TestSomething {


    @BeforeClass
    public static void runOnce(){
        Constants.getInstance().setRunType(RUN_TYPE.TEST);
    }

    @Test
    public void testOne(){
        // tests
    }
}
px06
  • 2,256
  • 1
  • 27
  • 47
0

You can pass parameters while executing a test through a CLI.

For example, if you want to pass the URL while executing the tests, you can do something like:

am instrument -w -r -e URL "http://www.google.com" -e debug false -e class com.example.android.TestClassName com.example.android.test/android.support.test.runner.AndroidJUnitRunner

To fetch the value of the same in your tests, you can use:

InstrumentationRegistry.getArguments().getString("URL")

For more information, you can read about -e here: Test from the Command Line

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55