0

I am new to instrumented tests and I am not able to have my very first test class to run. It is failing with this error:

java.lang.AssertionError: Activity never becomes requested state "[RESUMED, DESTROYED, STARTED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")

The test class is very simple, just contains a method to test a Fragment method that validates if the EditText are empty:

@RunWith(AndroidJUnit4.class)
public class CiudadFormularioFragmentInstrumentedTest {
    private FragmentScenario<CityFormFragment> fragmentScenario;

    @Before
    public void setUp() {
        // Launch the fragment
        fragmentScenario = FragmentScenario.launchInContainer(CityFormFragment.class);
    }

    @Test
    public void testValidate() {
        this.fragmentScenario.onFragment(CityFormFragment -> {
            assertTrue(CityFormFragment.validate());
        });
    }
}

The validate() method:

protected boolean validate() {
    boolean valid = true;
    for (int i = 0; i < this.mandatory.size(); i++) {
        View view = this.mandatory.get(i);
        if (view instanceof EditText) {
            if (TextUtils.isEmpty(((EditText) view).getText())) {
                ((EditText) view).setError(this.getResources()
                        .getString(R.string.missing_data));
                valid = false;
            }
        }
    }
    return valid;
}

Why test is failing with above error? I checked other posts with same error, but can't make mine to work.

By the way, I am running the test in an emulator.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.