简体   繁体   中英

Please help - Jetpack Compose Testing - Hilt - ViewModel - Repository

I am trying to learn how to test with Jetpack Compose and I'm feeling lost. I'm not sure what I am doing wrong. I want to test the MainScreen, but it is nested in a ScreenNavigation() and needs a ViewModel and a NavController. I'm really confused how this works. It is telling me it can't find the activity. Im not sure what to do in the AndroidManifest file. Any help is very much appreciated!

Error: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samm.brewerysearch.test/androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared?

<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>


<application
    android:name="com.samm.brewerysearch.BrewApplication"
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Bloomberg"
    tools:targetApi="31">
    <activity
        android:name="com.samm.brewerysearch.MainActivity"
        android:exported="true"
        android:theme="@style/Theme.Bloomberg">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
@HiltAndroidTest
@UninstallModules(AppModule::class)
class MainScreenTest {

    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeRule = createAndroidComposeRule<MainActivity>()

    @Before
    fun setUp() {
        hiltRule.inject()

        composeRule.setContent {
            val navController = rememberNavController()
            
            BreweryTheme {
                NavHost(
                    navController = navController,
                    startDestination = Screens.MainScreen.name
                ){
                    composable(Screens.MainScreen.name){
                        MainScreen(
                            navController = navController,
                            mainViewModel = hiltViewModel(),
                            search = Constants.DEFAULT_CITY
                        )
                    }
                }
            }
        }
    }


    @Test
    fun myTest(){
        composeRule.onNodeWithText(Constants.DEFAULT_CITY).assertIsDisplayed()
    }
}

Add these rule to test your screen and mock viremodel

  @get:Rule
     val initRule: MockitoRule = MockitoJUnit.rule() 

    @get:Rule
    val composeTestRule = createAndroidComposeRule<ComponentActivity>()
    
    @Mock
    lateinit var mainViewModel: MainViewModel

you can initialize your navcontroller by

this.navController  = rememberNavController()

furthermore ther's no need to add @HiltAndroidTest annotation to your class, replace this annotation with @RunWith(MockitoJUnitRunner::class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM