Skip to content Skip to sidebar Skip to footer

Is It Possible To Test An Abstract Activity With Robolectric

I use abstract activity classes in my code to well, abstract away some features from the activity classes. I'm trying to test the abstract activity classes using Robolectric and t

Solution 1:

The first error saying that you added non-default constructor to your test class or changed access level for default one. But as it says junit Test class should have at least one public constructor.

The second one says that at least one method in test class should have @Test annotation (junit 4) or starts with test substring (junit 3).

Solution 2:

Yo can doing exactly what you are trying to do: subclass the abstract activity and instance the concrete class.

However, you need to declare the class extending the abstract Activity in it's own public file. If it's a nested class Robolectric will fail to instance it.

I don't know why, though.

Solution 3:

I test an abstract activity this way:

1. Creating the abstract avtivity:

publicabstractclassAbstractActivityextendsAppCompatActivity {

    publicintgetNumber() {
        return2;
    }
}

2. Creating the test class: You just need to declare a static nested subclass of your abstract class.

@RunWith(RobolectricTestRunner.class)publicclassAbstractActivityTest {

    @TestpublicvoidcheckNumberReturn()throws Exception {
        TestAbstractActivitytestAbstractActivity= Robolectric.setupActivity(TestAbstractActivity.class);
        assertThat(testAbstractActivity.getNumber(), is(2));
    }

    publicstaticclassTestAbstractActivityextendsAbstractActivity {
    }
}

Post a Comment for "Is It Possible To Test An Abstract Activity With Robolectric"