Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I need to import to use launchActivity<>() in UnitTests?

So I'm trying to test my activity following googles instructions here: https://developer.android.com/guide/components/activities/testing

But the code launchActivity<MyActivity>() does not work. Do I need to define launchActivity as a rule or is there a library I need to import in gradle?

These are the imports I already have

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
like image 623
Dan Anderson Avatar asked Mar 30 '19 15:03

Dan Anderson


People also ask

Which package do we need to use for writing Android test cases?

You will useAndroid studio to create an Android application under a package com. tutorialspoint.

How can write unit test cases for activity in Android?

unit tests can be written under test java package, and instrumental tests can be written under androidTest package. You can then access UI widgets using Espresso ViewMatchers and then you can apply actions on them using ViewActions . Check documentation for further help. Show activity on this post.

Which Java framework is used to write unit tests in android?

JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.


1 Answers

You need to import following dependency into your gradle.

androidTestImplementation 'androidx.test:core-ktx:1.1.0'

Moreover, also add this in gradle file to avoid compile time error after adding launchActivity method in your test code.

kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
like image 85
NightFury Avatar answered Sep 27 '22 19:09

NightFury