项目
buildscript {
ext {
kotlin_version = "1.5.31"
hilt_version = "2.38.1"
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
字符串
(:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.priyanshumaurya8868.hilttesting"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.priyanshumaurya8868.hilttesting.HiltTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
androidTestImplementation "com.google.truth:truth:1.1.3"
}
型
- Inside(andorid test)TestRunner*
package com.priyanshumaurya8868.hilttesting
import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
class HiltTestRunner: AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, HiltTestRunner::class.java.name, context)
}
}
型
模块
package com.priyanshumaurya8868.hilttesting
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Module
@InstallIn(SingletonComponent::class)
object TestAppModule {
@Provides
fun provideRandomString()="random testing 1"
}
型
unitTest
package com.priyanshumaurya8868.hilttesting
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject
@SmallTest
@HiltAndroidTest
class TestString {
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
@Before
fun init(){
hiltRule.inject()
}
@Inject
lateinit var str :String
@Test
fun test_empty_string(){
assertThat(str).isNotEmpty()
}
}
**测试结果(0/0)*10/05 16:13:47:在HMD Global Nokia 6.1 Plus上启动“TestString”.应用重新启动成功,无需重新安装以下APK(S):com.priyanshumaurya8868.hilttesting运行测试
$ adb shell am instrument -w -m -e debug false -e class 'com.priyanshumaurya8868.hilttesting.TestString' com.priyanshumaurya8868.hilttesting.test/com.priyanshumaurya8868.hilttesting.HiltTestRunner已连接到设备'hmd_global-nokia_6_1_plus-DRGID 19032402270'上的进程26841。**
1条答案
按热度按时间ccgok5k51#
它应该是:
字符串
另一方面,你在这里有一个
HiltTestRunner
的循环。