You can use Robolectric for unit testing Android on the JVM.
Robolectric is an infrastructure that allows you to write unit tests and run them on the desktop JVM while still using the Android API.
Robolectric provides a JVM-compatible version of the android.jar file. Robolectric handles presentation inflation, resource loading, and much more, which is implemented in native C code on Android devices.
dependencies { ... // Robolectric testCompile "org.robolectric:robolectric:3.3.2" }
- Your tests should be stored in the
src/test directory. - The class containing your Robolectric test should be annotated with
@RunWith(RobolectricTestRunner.class runner). - It should also use @Config () to point to your BuildConfig.class class.
for example
@RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class) public class WelcomeActivityTest { private WelcomeActivity activity; @Before public void setUp() throws Exception { activity = Robolectric.buildActivity( WelcomeActivity.class ) .create() .resume() .get(); } @Test public void shouldNotBeNull() throws Exception { assertNotNull( activity ); } }
Find out more here.
yoAlex5 Jul 18 '19 at 20:31 2019-07-18 20:31
source share