user1754960
user1754960

Reputation: 141

How to abstract Robotium Setup to run multiple test files

In a Blackbox test environment I would need to include CODE 1 and end with CODE 2 to perform a test by running Android JUnit Test (as explained from the Robotium site):

CODE 1:

public class ConnectApp extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.example.android.notepad.NotesList";
private static Class<?> launcherActivityClass;
private Solo solo;
static { 
    try { launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); }
    catch (ClassNotFoundException e) { throw new RuntimeException(e); }
}
public ConnectApp() throws ClassNotFoundException {
    super(launcherActivityClass);
}
public void setUp() throws Exception {
    this.solo = new Solo(getInstrumentation(), getActivity());
}

CODE 2:

public void testNumberOne() { … }
public void testNumberTwo() { … }

}

However, I would like to abstract CODE 1 of the code ( which includes getInstrumentation() and getAcitvity()) so that I can simply call them in a separate test file and then run CODE 2 . This is because I want to have tests in separate files and don't want to keep adding the same amount of CODE 1 code but just call a method/constructor to initiate the process.

Is there a way to do this? Thank you in advance.

Upvotes: 0

Views: 946

Answers (1)

Paul Harris
Paul Harris

Reputation: 5819

yes there is a way to do this. What you will need to do is create an empty test class such as:

public class TestTemplate extends ActivityInstrumentationTestCase2 {

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.example.android.notepad.NotesList";
    private static Class<?> launcherActivityClass;
    private Solo solo;

    static { 
        try { launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); }
        catch (ClassNotFoundException e) { throw new RuntimeException(e); }
    }
    public ConnectApp() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    public void setUp() throws Exception {
        super.setUp();//I added this line in, you need it otherwise things might go wrong
        this.solo = new Solo(getInstrumentation(), getActivity());
    }

    public Solo getSolo(){
        return solo;
    }
}

Then for every test class you want in the future instead of extending ActivityInstrumentationTestCase2 you will extend TestTemplate.

for example:

public class ActualTest extends TestTemplate {
    public ActualTest() throws ClassNotFoundException {
    super();
}

    public void setUp() throws Exception {
        super.setUp();
        //anything specific to setting up for this test
    }

    public void testNumberOne() { … }
    public void testNumberTwo() { … }
}

Upvotes: 2

Related Questions