2

I'm testing my app's Login with Robotium and I'm having some problems. I've got two different user types so I've made 3 tests. One for each type and another failed login. If I execute them individually they all success, but if I run all the test case it runs one, executes the tearDown(solo.finishOpenedActivities) and it doesn't restart the activity to execute the other tests. So in the second test when I'm asking for a EditText it says that is not available.

Here is my code:

public class TestLogin extends ActivityInstrumentationTestCase2<MainActivity> {

private Solo solo;

public TestLogin() {
    super("com.truekke4.test", MainActivity.class);
}

@Override
protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    super.setUp();
}

@Override
public void tearDown() throws Exception {
    getActivity().logout();
    solo.finishOpenedActivities();
    super.tearDown();
}

public void testUsuarioDesconocido() {
    solo.clearEditText(0);
    solo.enterText(0, "usuario desconocido");
    solo.assertCurrentActivity("Error", MainActivity.class);
    solo.clickOnButton("OK");
    solo.clickOnButton("OK");
    solo.assertCurrentActivity("Error", MainActivity.class);
}

public void testUsuario() {
    solo.clearEditText(0);
    solo.enterText(0, "usuario");
    solo.clickOnButton("OK");
    solo.assertCurrentActivity("Error", InicioUsuarioActivity.class);
}

public void testEmpresa() {
    solo.clearEditText(0);
    solo.enterText(0, "empresa");
    solo.clickOnButton("OK");
    solo.assertCurrentActivity("Error", InicioPymeActivity.class);
}

}

I've got to restart the activity manually? Create and Intent and startActivity(intent)?

I don't have to finish opened activities? Or I have to finish activities but restart them/it in setUp(). How Can I restart or create activities to make recognizeables for Robotium?

Help!

Jon Zangitu
  • 957
  • 1
  • 15
  • 30
  • I think you should logout in your tested methods, otherwise it can be logged in every next method. (If you login into some app and close it, it won't logout - here is the same case) – maszter Jan 30 '13 at 17:49

2 Answers2

3

I believe your issue is being caused because you're overriding the setUp() method from ActivityInstrumentationTestCase2 rather than using the Robotium method. In your test classes, Robotium looks for a method with the signature public void setUp() to configure its tests, which can get confusing, because it has the same name as the method that can be overridden. I'm not sure of exactly when or how often the protected setUp is called, but I know that the public one is the best place to initialize solo as Robotium is guaranteed to call it before each individual test. Try changing:

@Override
protected void setUp() throws Exception

to:

public void setUp() throws Exception

with the same method body (but without the @Override annotation) and see if that allows you to run all your tests.

MattDavis
  • 5,158
  • 2
  • 23
  • 35
  • Thank you Matt, that was the problem! – Jon Zangitu Feb 05 '13 at 10:45
  • I'm assuming it was the change from protected to public that fixed this. The method probably needs to be public to be called by the test runner. Changing the annotation can't possibly have been the reason (...right?) – ZoFreX Feb 17 '14 at 18:59
2

where you have the code:

@Override
protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    super.setUp();
}

make it:

@Override
protected void setUp() throws Exception {
    super.setUp();
    solo = new Solo(getInstrumentation(), getActivity());
}

if this does not work for some reason your class isn't getting scrubbed properly in the teardown, you can either call setActivity(null) to make getActicity() launch the activity again or manually call launchActivity yourself.

Paul Harris
  • 5,769
  • 1
  • 25
  • 41
  • Thank you Paul, but it didn't work any of your suggestions. I've tried changing the order of super call, but it worked worse, the tearDown failed. I've tried with setActivity(null), setActivity()... nothing. I suppose that if I make solo.finishOpenedActivities() I'm finishing test activity and it gives me an error. But, what can I do to go back to the initial Activity? Restart the state of the activity to simulate a first entrance. – Jon Zangitu Jan 31 '13 at 09:25