Is it possible to check abstract activity with Robolectric

I use abstract activity classes in my code to distract some functions from activity classes.

I am trying to test abstract activity classes using Robolectric and gradle-android-test-plugin using subclasses extending the abstract class. However, I cannot get it to work.

Does anyone have experience in this area, and is it even possible? The basic structure:

 @RunWith(RobolectricGradleTestRunner.class) public class AbstractActivityTest { private ActivityTest activity; @Before public void setUp() throws Exception { activity = Robolectric.buildActivity(ActivityTest.class).create().get(); } private class ActivityTest extends AbstractActivity { // do something } } 

I initially received the error message that the subclass was not static, so I made it static. Now I get the following two crashes:

 initializationError FAILED java.lang.Exception: Test class should have exactly one public constructor initializationError FAILED java.lang.Exception: No runnable methods 

Any obviously true tests that I put in @Test methods succeed.

+4
source share
3 answers

The first error indicating that you added a default constructor to your test class or changed the default access level. But as they say, the junit Test class must have at least one public constructor.

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

+5
source

Yo can do exactly what you are trying to do: a subclass of abstract activity and an instance of a particular class.

However, you need to declare a class that extends abstract activity in its own public file. If it is a nested Robolectric class it will not be able to instantiate it.

I do not know why.

+3
source

I test the abstract activity as follows:

1. Creating an abstract ability:

 public abstract class AbstractActivity extends AppCompatActivity { public int getNumber() { return 2; } } 

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

 @RunWith(RobolectricTestRunner.class) public class AbstractActivityTest { @Test public void checkNumberReturn() throws Exception { TestAbstractActivity testAbstractActivity = Robolectric.setupActivity(TestAbstractActivity.class); assertThat(testAbstractActivity.getNumber(), is(2)); } public static class TestAbstractActivity extends AbstractActivity { } } 
0
source

Source: https://habr.com/ru/post/1500023/


All Articles