Reputation: 175
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!
Upvotes: 1
Views: 2789
Reputation: 7761
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
Upvotes: 0
Reputation: 2326
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
}
}
Upvotes: 1