kandi background
Explore Kits

robolectric | Android Unit Testing Framework | Unit Testing library

 by   robolectric Java Version: robolectric-4.8-alpha-1 License: Non-SPDX

 by   robolectric Java Version: robolectric-4.8-alpha-1 License: Non-SPDX

Download this library from

kandi X-RAY | robolectric Summary

robolectric is a Java library typically used in Testing, Unit Testing applications. robolectric has no bugs, it has no vulnerabilities, it has build file available and it has high support. However robolectric has a Non-SPDX License. You can download it from GitHub, Maven.
Robolectric is the industry-standard unit testing framework for Android. With Robolectric, your tests run in a simulated Android environment inside a JVM, without the overhead and flakiness of an emulator. Robolectric tests routinely run 10x faster than those on cold-started emulators. Robolectric supports running unit tests for 15 different versions of Android, ranging from Jelly Bean (API level 16) to S (API level 31).
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • robolectric has a highly active ecosystem.
  • It has 5442 star(s) with 1307 fork(s). There are 209 watchers for this library.
  • There were 1 major release(s) in the last 6 months.
  • There are 487 open issues and 2033 have been closed. On average issues are closed in 165 days. There are 195 open pull requests and 0 closed requests.
  • It has a negative sentiment in the developer community.
  • The latest version of robolectric is robolectric-4.8-alpha-1
robolectric Support
Best in #Unit Testing
Average in #Unit Testing
robolectric Support
Best in #Unit Testing
Average in #Unit Testing

quality kandi Quality

  • robolectric has 0 bugs and 0 code smells.
robolectric Quality
Best in #Unit Testing
Average in #Unit Testing
robolectric Quality
Best in #Unit Testing
Average in #Unit Testing

securitySecurity

  • robolectric has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • robolectric code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
robolectric Security
Best in #Unit Testing
Average in #Unit Testing
robolectric Security
Best in #Unit Testing
Average in #Unit Testing

license License

  • robolectric has a Non-SPDX License.
  • Non-SPDX licenses can be open source with a non SPDX compliant license, or non open source licenses, and you need to review them closely before use.
robolectric License
Best in #Unit Testing
Average in #Unit Testing
robolectric License
Best in #Unit Testing
Average in #Unit Testing

buildReuse

  • robolectric releases are available to install and integrate.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • Installation instructions, examples and code snippets are available.
  • robolectric saves you 184059 person hours of effort in developing the same functionality from scratch.
  • It has 201889 lines of code, 20032 functions and 1859 files.
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
robolectric Reuse
Best in #Unit Testing
Average in #Unit Testing
robolectric Reuse
Best in #Unit Testing
Average in #Unit Testing
Top functions reviewed by kandi - BETA

kandi has reviewed robolectric and discovered the below as its top functions. This is intended to give you an instant insight into robolectric implemented functionality, and help decide if they suit your requirements.

  • Dumps the contents of the View .
  • Formats a date with the specified locale .
  • Parses a package .
  • Is the requested resource table?
  • Execute the request .
  • Apply style attribute .
  • Set the contents of this string to a given buffer .
  • Create a package from an android manifest .
  • Get an entry from the cache .
  • Intercept the method that invokes a method without invocations .

robolectric Key Features

Android Unit Testing Framework

Usage

copy iconCopydownload iconDownload
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {

  @Test
  public void clickingButton_shouldChangeResultsViewText() throws Exception {
    Activity activity = Robolectric.setupActivity(MyActivity.class);

    Button button = (Button) activity.findViewById(R.id.press_me_button);
    TextView results = (TextView) activity.findViewById(R.id.results_text_view);

    button.performClick();
    assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
  }
}

Starting a New Project

copy iconCopydownload iconDownload
testImplementation "junit:junit:4.13.2"
testImplementation "org.robolectric:robolectric:4.7.3"

build and test

copy iconCopydownload iconDownload
./gradlew clean assemble testClasses --parallel

Using Snapshots

copy iconCopydownload iconDownload
repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

dependencies {
    testImplementation "org.robolectric:robolectric:4.8-SNAPSHOT"
}

How to assert next started activity for a composable test?

copy iconCopydownload iconDownload
lateinit var context : Context
composeTestRule.setContent {
    context = LocalContext.current
    MyComposableToTest()
}
val shadowActivity: ShadowActivity = Shadow.extract(context as ComponentActivity)
val nextStartedActivity = shadowActivity.nextStartedActivity
assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, nextStartedActivity.action)
-----------------------
lateinit var context : Context
composeTestRule.setContent {
    context = LocalContext.current
    MyComposableToTest()
}
val shadowActivity: ShadowActivity = Shadow.extract(context as ComponentActivity)
val nextStartedActivity = shadowActivity.nextStartedActivity
assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, nextStartedActivity.action)
-----------------------
androidTestImplementation("androidx.test.espresso:espresso-intents:3.4.0")
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
// ...

@RunWith(AndroidJUnit4::class)
class MainActivityInstrumentedTest {
    @get:Rule val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Test fun testTheIntent() {
        Intents.init() // IMPORTANT (to be able to use "intended" function)
        composeTestRule.setContent {
            MyAppTheme {
                MainScreen()
            }
        }
        composeTestRule.onNodeWithText("My Button").performClick()
        intended(hasComponent(MySecondActivity::class.java.name))
    }
}
-----------------------
androidTestImplementation("androidx.test.espresso:espresso-intents:3.4.0")
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
// ...

@RunWith(AndroidJUnit4::class)
class MainActivityInstrumentedTest {
    @get:Rule val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Test fun testTheIntent() {
        Intents.init() // IMPORTANT (to be able to use "intended" function)
        composeTestRule.setContent {
            MyAppTheme {
                MainScreen()
            }
        }
        composeTestRule.onNodeWithText("My Button").performClick()
        intended(hasComponent(MySecondActivity::class.java.name))
    }
}

Why does ExceptionInInitializerError occur when invoking HtmlCompat in an Android unit test using Robolectric?

copy iconCopydownload iconDownload
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }

App is not able to find DynamicFeature navigation graph. Getting Resources$NotFoundException for the navigation graph

copy iconCopydownload iconDownload
override fun attachBaseContext(newBase: Context) {
    super.attachBaseContext(newBase)
    SplitCompat.installActivity(this)
}

Test if a webpage was opened succesfully via intent using Robolectric

copy iconCopydownload iconDownload
    val shadow = Shadows.shadowOf(activity)
    val exp_url = "www.google.com"
    val intent =  shadow.nextStartedActivity
    val url = intent.data.toString()
    assertEquals(exp_url,url)

Unable to use performclick() on customview in Roboelectric testcase?

copy iconCopydownload iconDownload
@Test
fun checkNavigation(){
    val myCustomView = activity.agent_floating_row
    val eventListener = RowPhoto::class.java.getDeclaredField("rowClick")
    eventListener.isAccessible = true

    val clickInterface = eventListener.get(myCustomView) as RowEventCallback
    clickInterface.onRowclick()
}

Mockk library CoroutineScope ClassCastException

copy iconCopydownload iconDownload
    fun getProfileInfo() = launchOn {
        executeProfileInfoGet()
    }

    suspend fun executeProfileInfoGet(){
        executeFlow(profileInfoRepository.getProfileInfo()) {
            when (it) {
                is DataState.Success -> {
                    initCaches(it.response)
                    checkPassportFields(it.response)
                    profileInfoLiveData.postValue(it.response)
                }
                is DataState.Error -> {
                    showError(it.error.message)
                }
                else -> Unit
            }
        }
    }
coEvery { viewModel.executeProfileInfoGet() } coAnswers { callOriginal() }
-----------------------
    fun getProfileInfo() = launchOn {
        executeProfileInfoGet()
    }

    suspend fun executeProfileInfoGet(){
        executeFlow(profileInfoRepository.getProfileInfo()) {
            when (it) {
                is DataState.Success -> {
                    initCaches(it.response)
                    checkPassportFields(it.response)
                    profileInfoLiveData.postValue(it.response)
                }
                is DataState.Error -> {
                    showError(it.error.message)
                }
                else -> Unit
            }
        }
    }
coEvery { viewModel.executeProfileInfoGet() } coAnswers { callOriginal() }

Robolectric can't find new changes to Framework

copy iconCopydownload iconDownload
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)
-----------------------
@Config(sdk = Build.VERSION_CODES.P)
java.lang.UnsupportedOperationException: Robolectric does not support API level 28.
include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
include external/robolectric-shadows/run_robotests.mk
#########################
# Robolectric test target
#########################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_JAVA_LIBRARIES := \
    mockito-robolectric-prebuilt \
    junit \
    Robolectric_all-target \

LOCAL_INSTRUMENTATION_FOR := TheService
LOCAL_MODULE := TheServiceRoboTests
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_JAVA_LIBRARY)

###########################
# Robolectric runner target
###########################
include $(CLEAR_VARS)

LOCAL_MODULE := RunTheServiceRoboTests
LOCAL_SDK_VERSION := current
LOCAL_STATIC_JAVA_LIBRARIES := TheServiceRoboTests
LOCAL_TEST_PACKAGE := TheService

LOCAL_JAVA_LIBRARIES := \
    TheServiceRoboTests \
    guava \
    Robolectric_all-target \
    robolectric_android-all-stub \
    mockito-robolectric-prebuilt \

include external/robolectric-shadows/run_robotests.mk
@Config(sdk = Build.VERSION_CODES.CUR_DEVELOPMENT)
new HashMap<Integer, SdkVersion>() {
{
  addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "r1", "REL");
  addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "r1", "REL");
  addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "r2", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.2_r3", "r0", "REL");
  addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "r2", "REL");
  addSdk(Build.VERSION_CODES.M, "6.0.1_r3", "r1", "REL");
  addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "r1", "REL");
  addSdk(Build.VERSION_CODES.N_MR1, "7.1.0_r7", "r1", "REL");
  addSdk(Build.VERSION_CODES.O, "8.0.0_r4", "r1", "REL");
  addSdk(Build.VERSION_CODES.O_MR1, "8.1.0", "4611349", "REL");
  addSdk(Build.VERSION_CODES.P, "9", "4913185-2", "REL");
  addSdk(Build.VERSION_CODES.Q, "10", "5803371", "REL");
  // BEGIN-INTERNAL
  // TODO: Update jar with final R release.
  addSdk(Build.VERSION_CODES.R, "R-beta2", "6625208", "REL");
  addSdk(Build.VERSION_CODES.S, "S", "r0", "S");
  // END-INTERNAL
}
public DependencyJar getAndroidSdkDependency() {
    return createDependency("org.robolectric", "android-all", getSdkVersion().getAndroidVersion() + "-robolectric-" + getSdkVersion().getRobolectricVersion(), null);
}
@Config(sdk = Build.VERSION_CODES.S)

Error inflating ViewBinding in test class : Binary XML file line #38: Binary XML file line #38: Error inflating class &lt;unknown&gt;

copy iconCopydownload iconDownload
public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final Button getText;

  @NonNull
  public final ProgressBar progress;

  @NonNull
  public final TextView text;

  private ActivityMainBinding(@NonNull ConstraintLayout rootView, @NonNull Button getText,
      @NonNull ProgressBar progress, @NonNull TextView text) {
    this.rootView = rootView;
    this.getText = getText;
    this.progress = progress;
    this.text = text;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.get_text;
      Button getText = ViewBindings.findChildViewById(rootView, id);
      if (getText == null) {
        break missingId;
      }

      id = R.id.progress;
      ProgressBar progress = ViewBindings.findChildViewById(rootView, id);
      if (progress == null) {
        break missingId;
      }

      id = R.id.text;
      TextView text = ViewBindings.findChildViewById(rootView, id);
      if (text == null) {
        break missingId;
      }

      return new ActivityMainBinding((ConstraintLayout) rootView, getText, progress, text);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}
@Before
fun setUp() {
    // return the mock root from the mock inflater
    doReturn(mMockConvertView).`when`(mMockInflater).inflate(R.layout.my_layout, mMockViewGroup, false)
    
    // extra mocks to handle findChildViewById
    doReturn(1).`when`(mMockConvertView).childCount
    doReturn(mMockConvertView).`when`(mMockConvertView).getChildAt(0)

    // Return the mocked views
    doReturn(mMockText).`when`(mMockConvertView).findViewById<View>(R.id.text)
    doReturn(mMockButton).`when`(mMockConvertView).findViewById<View>(R.id.get_text)
    doReturn(mMockProgBar).`when`(mMockConvertView).findViewById<View>(R.id.progress)
}
@Nullable
public static <T extends View> T findChildViewById(View rootView, @IdRes int id) {
    if (!(rootView instanceof ViewGroup)) {
        return null;
    }
    final ViewGroup rootViewGroup = (ViewGroup) rootView;
    final int childCount = rootViewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final T view = rootViewGroup.getChildAt(i).findViewById(id);
        if (view != null) {
            return view;
        }
    }
    return null;
}
val binding = ActivityMainBinding.inflate(mMockInflater, mMockViewGroup, false)
-----------------------
public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final Button getText;

  @NonNull
  public final ProgressBar progress;

  @NonNull
  public final TextView text;

  private ActivityMainBinding(@NonNull ConstraintLayout rootView, @NonNull Button getText,
      @NonNull ProgressBar progress, @NonNull TextView text) {
    this.rootView = rootView;
    this.getText = getText;
    this.progress = progress;
    this.text = text;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.get_text;
      Button getText = ViewBindings.findChildViewById(rootView, id);
      if (getText == null) {
        break missingId;
      }

      id = R.id.progress;
      ProgressBar progress = ViewBindings.findChildViewById(rootView, id);
      if (progress == null) {
        break missingId;
      }

      id = R.id.text;
      TextView text = ViewBindings.findChildViewById(rootView, id);
      if (text == null) {
        break missingId;
      }

      return new ActivityMainBinding((ConstraintLayout) rootView, getText, progress, text);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}
@Before
fun setUp() {
    // return the mock root from the mock inflater
    doReturn(mMockConvertView).`when`(mMockInflater).inflate(R.layout.my_layout, mMockViewGroup, false)
    
    // extra mocks to handle findChildViewById
    doReturn(1).`when`(mMockConvertView).childCount
    doReturn(mMockConvertView).`when`(mMockConvertView).getChildAt(0)

    // Return the mocked views
    doReturn(mMockText).`when`(mMockConvertView).findViewById<View>(R.id.text)
    doReturn(mMockButton).`when`(mMockConvertView).findViewById<View>(R.id.get_text)
    doReturn(mMockProgBar).`when`(mMockConvertView).findViewById<View>(R.id.progress)
}
@Nullable
public static <T extends View> T findChildViewById(View rootView, @IdRes int id) {
    if (!(rootView instanceof ViewGroup)) {
        return null;
    }
    final ViewGroup rootViewGroup = (ViewGroup) rootView;
    final int childCount = rootViewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final T view = rootViewGroup.getChildAt(i).findViewById(id);
        if (view != null) {
            return view;
        }
    }
    return null;
}
val binding = ActivityMainBinding.inflate(mMockInflater, mMockViewGroup, false)
-----------------------
public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final Button getText;

  @NonNull
  public final ProgressBar progress;

  @NonNull
  public final TextView text;

  private ActivityMainBinding(@NonNull ConstraintLayout rootView, @NonNull Button getText,
      @NonNull ProgressBar progress, @NonNull TextView text) {
    this.rootView = rootView;
    this.getText = getText;
    this.progress = progress;
    this.text = text;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.get_text;
      Button getText = ViewBindings.findChildViewById(rootView, id);
      if (getText == null) {
        break missingId;
      }

      id = R.id.progress;
      ProgressBar progress = ViewBindings.findChildViewById(rootView, id);
      if (progress == null) {
        break missingId;
      }

      id = R.id.text;
      TextView text = ViewBindings.findChildViewById(rootView, id);
      if (text == null) {
        break missingId;
      }

      return new ActivityMainBinding((ConstraintLayout) rootView, getText, progress, text);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}
@Before
fun setUp() {
    // return the mock root from the mock inflater
    doReturn(mMockConvertView).`when`(mMockInflater).inflate(R.layout.my_layout, mMockViewGroup, false)
    
    // extra mocks to handle findChildViewById
    doReturn(1).`when`(mMockConvertView).childCount
    doReturn(mMockConvertView).`when`(mMockConvertView).getChildAt(0)

    // Return the mocked views
    doReturn(mMockText).`when`(mMockConvertView).findViewById<View>(R.id.text)
    doReturn(mMockButton).`when`(mMockConvertView).findViewById<View>(R.id.get_text)
    doReturn(mMockProgBar).`when`(mMockConvertView).findViewById<View>(R.id.progress)
}
@Nullable
public static <T extends View> T findChildViewById(View rootView, @IdRes int id) {
    if (!(rootView instanceof ViewGroup)) {
        return null;
    }
    final ViewGroup rootViewGroup = (ViewGroup) rootView;
    final int childCount = rootViewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final T view = rootViewGroup.getChildAt(i).findViewById(id);
        if (view != null) {
            return view;
        }
    }
    return null;
}
val binding = ActivityMainBinding.inflate(mMockInflater, mMockViewGroup, false)
-----------------------
public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final Button getText;

  @NonNull
  public final ProgressBar progress;

  @NonNull
  public final TextView text;

  private ActivityMainBinding(@NonNull ConstraintLayout rootView, @NonNull Button getText,
      @NonNull ProgressBar progress, @NonNull TextView text) {
    this.rootView = rootView;
    this.getText = getText;
    this.progress = progress;
    this.text = text;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.get_text;
      Button getText = ViewBindings.findChildViewById(rootView, id);
      if (getText == null) {
        break missingId;
      }

      id = R.id.progress;
      ProgressBar progress = ViewBindings.findChildViewById(rootView, id);
      if (progress == null) {
        break missingId;
      }

      id = R.id.text;
      TextView text = ViewBindings.findChildViewById(rootView, id);
      if (text == null) {
        break missingId;
      }

      return new ActivityMainBinding((ConstraintLayout) rootView, getText, progress, text);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}
@Before
fun setUp() {
    // return the mock root from the mock inflater
    doReturn(mMockConvertView).`when`(mMockInflater).inflate(R.layout.my_layout, mMockViewGroup, false)
    
    // extra mocks to handle findChildViewById
    doReturn(1).`when`(mMockConvertView).childCount
    doReturn(mMockConvertView).`when`(mMockConvertView).getChildAt(0)

    // Return the mocked views
    doReturn(mMockText).`when`(mMockConvertView).findViewById<View>(R.id.text)
    doReturn(mMockButton).`when`(mMockConvertView).findViewById<View>(R.id.get_text)
    doReturn(mMockProgBar).`when`(mMockConvertView).findViewById<View>(R.id.progress)
}
@Nullable
public static <T extends View> T findChildViewById(View rootView, @IdRes int id) {
    if (!(rootView instanceof ViewGroup)) {
        return null;
    }
    final ViewGroup rootViewGroup = (ViewGroup) rootView;
    final int childCount = rootViewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final T view = rootViewGroup.getChildAt(i).findViewById(id);
        if (view != null) {
            return view;
        }
    }
    return null;
}
val binding = ActivityMainBinding.inflate(mMockInflater, mMockViewGroup, false)

Robolectric Handler post() not working as expected

copy iconCopydownload iconDownload
@Test
    fun `test do something`() {
        val underTest = ClassToTest()
        underTest.doSomething()
        shadowOf(getMainLooper()).idle()
    }

cannot find symbol import dagger.hilt.android.internal.Contexts

copy iconCopydownload iconDownload
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "com.google.dagger:hilt-android:2.38.1"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
-----------------------
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "com.google.dagger:hilt-android:2.38.1"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
-----------------------
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "com.google.dagger:hilt-android:2.38.1"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'

Community Discussions

Trending Discussions on robolectric
  • How to assert next started activity for a composable test?
  • Why does ExceptionInInitializerError occur when invoking HtmlCompat in an Android unit test using Robolectric?
  • App is not able to find DynamicFeature navigation graph. Getting Resources$NotFoundException for the navigation graph
  • Test if a webpage was opened succesfully via intent using Robolectric
  • Unable to use performclick() on customview in Roboelectric testcase?
  • Mockk library CoroutineScope ClassCastException
  • Robolectric can't find new changes to Framework
  • Error inflating ViewBinding in test class : Binary XML file line #38: Binary XML file line #38: Error inflating class &lt;unknown&gt;
  • Robolectric ShadowAsyncTask this action is not supported in PAUSED mode
  • Robolectric Handler post() not working as expected
Trending Discussions on robolectric

QUESTION

How to assert next started activity for a composable test?

Asked 2022-Apr-03 at 14:38

I have a composable with a button that launches one of the native activities (Google Settings). To test this before compose (using Robolectric) I would do something like this:

My test:

@Test
fun `MyFragment - when button clicked - starts activity`() {
    // ...
    val shadowActivity: ShadowActivity = Shadow.extract(activity)
    val nextStartedActivity = shadowActivity.nextStartedActivity
    assertNotNull(nextStartedActivity)
    assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, nextStartedActivity.action)
}

With compose tests (not using activity scenario) it's different. There is no activity handle, only a composeTestRule:

My test:

// ...

private val buttonNode get() = composeTestRule.onNodeWithContentDescription("Button")

@Test
fun `MyComposableToTest - when button clicked - starts activity`() {
    composeTestRule.setContent {
        MyComposableToTest()
    }

    buttonNode.assertExists().assertHasClickAction().assertIsEnabled().performClick()

    // No possibility to get current activity
}

How can I assert that a new activity is started when testing a Composable?

Some context:

  • Android Gradle Plugin 7.0.3
  • Robolectric 4.7.3
  • Compose 1.1.0-beta04

ANSWER

Answered 2021-Dec-09 at 08:52

You are able to fetch the context from the ComposeContentTestRule like this:

lateinit var context : Context
composeTestRule.setContent {
    context = LocalContext.current
    MyComposableToTest()
}

and then to assert the next started activity

val shadowActivity: ShadowActivity = Shadow.extract(context as ComponentActivity)
val nextStartedActivity = shadowActivity.nextStartedActivity
assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, nextStartedActivity.action)

Source https://stackoverflow.com/questions/70286818

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install robolectric

Robolectric supports running tests against multiple Android API levels. The work it must do to support each API level is slightly different, so its shadows are built separately for each. To build shadows for every API version, run:.
If you would like to live on the bleeding edge, you can try running against a snapshot build. Keep in mind that snapshots represent the most recent changes on master and may contain bugs.

Support

Robolectric is built using Gradle. Both IntelliJ and Android Studio can import the top-level build.gradle file and will automatically generate their project files from it.

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Explore Related Topics

Share this Page

share link
Reuse Pre-built Kits with robolectric
Consider Popular Unit Testing Libraries
Compare Unit Testing Libraries with Highest Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.