Hilt 테스트 가이드

Hilt와 같은 종속 항목 삽입 프레임워크를 사용하여 얻을 수 있는 이점 중 하나는 코드를 더 쉽게 테스트할 수 있다는 점입니다.

단위 테스트

생성자 삽입을 사용하는 클래스를 테스트할 때 이 클래스를 인스턴스화하는 데 Hilt를 사용할 필요가 없기 때문에 단위 테스트에는 Hilt가 필요하지 않습니다. 대신 생성자에 주석이 지정되지 않은 경우와 마찬가지로 가짜 또는 모의 종속 항목을 전달하여 클래스 생성자를 직접 호출할 수 있습니다.

Kotlin

@ActivityScoped
class AnalyticsAdapter @Inject constructor(
  private val service: AnalyticsService
) { ... }

class AnalyticsAdapterTest {

  @Test
  fun `Happy path`() {
    // You don't need Hilt to create an instance of AnalyticsAdapter.
    // You can pass a fake or mock AnalyticsService.
    val adapter = AnalyticsAdapter(fakeAnalyticsService)
    assertEquals(...)
  }
}

Java

@ActivityScope
public class AnalyticsAdapter {

  private final AnalyticsService analyticsService;

  @Inject
  AnalyticsAdapter(AnalyticsService analyticsService) {
    this.analyticsService = analyticsService;
  }
}

public final class AnalyticsAdapterTest {

  @Test
  public void happyPath() {
    // You don't need Hilt to create an instance of AnalyticsAdapter.
    // You can pass a fake or mock AnalyticsService.
    AnalyticsAdapter adapter = new AnalyticsAdapter(fakeAnalyticsService);
    assertEquals(...);
  }
}

엔드 투 엔드 테스트

통합 테스트의 경우 Hilt는 프로덕션 코드에서와 같이 종속 항목을 삽입합니다. Hilt는 각 테스트의 새로운 구성요소 세트를 자동으로 생성하므로 Hilt를 사용한 테스트에는 유지보수가 필요하지 않습니다.

테스트 종속 항목 추가

테스트에서 Hilt를 사용하려면 다음과 같이 프로젝트에 hilt-android-testing 종속 항목을 포함합니다.

Groovy

dependencies {
    // For Robolectric tests.
    testImplementation 'com.google.dagger:hilt-android-testing:2.44'
    // ...with Kotlin.
    kaptTest 'com.google.dagger:hilt-android-compiler:2.44'
    // ...with Java.
    testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.44'


    // For instrumented tests.
    androidTestImplementation 'com.google.dagger:hilt-android-testing:2.44'
    // ...with Kotlin.
    kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.44'
    // ...with Java.
    androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.44'
}

Kotlin

dependencies {
    // For Robolectric tests.
    testImplementation("com.google.dagger:hilt-android-testing:2.44")
    // ...with Kotlin.
    kaptTest("com.google.dagger:hilt-android-compiler:2.44")
    // ...with Java.
    testAnnotationProcessor("com.google.dagger:hilt-android-compiler:2.44")


    // For instrumented tests.
    androidTestImplementation("com.google.dagger:hilt-android-testing:2.44")
    // ...with Kotlin.
    kaptAndroidTest("com.google.dagger:hilt-android-compiler:2.44")
    // ...with Java.
    androidTestAnnotationProcessor("com.google.dagger:hilt-android-compiler:2.44")
}

UI 테스트 설정

@HiltAndroidTest와 함께 Hilt를 사용하는 UI 테스트에 주석을 지정해야 합니다. 이 주석은 각 테스트에 관한 Hilt 구성요소 생성을 담당합니다.

또한 테스트 클래스에 HiltAndroidRule을 추가해야 합니다. HiltAndroidRule은 구성요소의 상태를 관리하고, 테스트에서 삽입을 실행하는 데 사용됩니다.

Kotlin

@HiltAndroidTest
class SettingsActivityTest {

  @get:Rule
  var hiltRule = HiltAndroidRule(this)

  // UI tests here.
}

Java

@HiltAndroidTest
public final class SettingsActivityTest {

  @Rule
  public HiltAndroidRule hiltRule = new HiltAndroidRule(this);

  // UI tests here.
}

다음으로, 테스트는 Hilt가 자동으로 생성하는 Application 클래스에 관해 알아야 합니다.

테스트 애플리케이션

Hilt를 지원하는 Application 객체에서 Hilt를 사용하는 계측 테스트를 실행해야 합니다. 라이브러리는 테스트에 사용할 HiltTestApplication을 제공합니다. 테스트에 다른 기본 애플리케이션이 필요하다면 테스트용 맞춤 애플리케이션을 참고하세요.

계측 테스트 또는 Robolectric 테스트에서 실행되도록 테스트 애플리케이션을 설정해야 합니다. 다음 안내는 Hilt에만 관련된 것이 아니라 테스트에서 실행할 맞춤 애플리케이션을 지정하는 방법에 관한 일반적인 가이드라인입니다.

계측 테스트에서 테스트 애플리케이션 설정

계측 테스트에서 Hilt 테스트 애플리케이션을 사용하려면 새 테스트 실행기를 구성해야 합니다. 이렇게 하면 Hilt는 프로젝트의 모든 계측 테스트에서 작동합니다. 다음 단계를 따르세요.

  1. androidTest 폴더에서 AndroidJUnitRunner를 확장하는 맞춤 클래스를 생성합니다.
  2. newApplication 함수를 재정의하고 생성된 Hilt 테스트 애플리케이션의 이름을 전달합니다.

Kotlin

// A custom runner to set up the instrumented application class for tests.
class CustomTestRunner : AndroidJUnitRunner() {

    override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
        return super.newApplication(cl, HiltTestApplication::class.java.name, context)
    }
}

Java

// A custom runner to set up the instrumented application class for tests.
public final class CustomTestRunner extends AndroidJUnitRunner {

  @Override
  public Application newApplication(ClassLoader cl, String className, Context context)
      throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    return super.newApplication(cl, HiltTestApplication.class.getName(), context);
  }
}

그런 다음, 계측 단위 테스트 가이드에 설명된 대로 Gradle 파일에서 이 테스트 실행기를 구성합니다. 다음과 같이 전체 클래스 경로를 사용해야 합니다.

Groovy

android {
    defaultConfig {
        // Replace com.example.android.dagger with your class path.
        testInstrumentationRunner "com.example.android.dagger.CustomTestRunner"
    }
}

Kotlin

android {
    defaultConfig {
        // Replace com.example.android.dagger with your class path.
        testInstrumentationRunner = "com.example.android.dagger.CustomTestRunner"
    }
}
Robolectric 테스트에서 테스트 애플리케이션 설정

Robolectric을 사용하여 UI 레이어를 테스트한다면 다음과 같이 robolectric.properties 파일에서 사용할 애플리케이션을 지정할 수 있습니다.

application = dagger.hilt.android.testing.HiltTestApplication

또는 다음과 같이 Robolectric의 @Config 주석을 사용하여 각 테스트에 애플리케이션을 개별적으로 구성할 수 있습니다.

Kotlin

@HiltAndroidTest
@Config(application = HiltTestApplication::class)
class SettingsActivityTest {

  @get:Rule
  var hiltRule = HiltAndroidRule(this)

  // Robolectric tests here.
}

Java

@HiltAndroidTest
@Config(application = HiltTestApplication.class)
class SettingsActivityTest {

  @Rule public HiltAndroidRule hiltRule = new HiltAndroidRule(this);

  // Robolectric tests here.
}

Android Gradle 플러그인 4.2 미만 버전을 사용하는 경우 모듈의 build.gradle 파일에 다음 구성을 적용하여 로컬 단위 테스트에서 @AndroidEntryPoint 클래스 변환을 사용 설정합니다.

Groovy

hilt {
    enableTransformForLocalTests = true
}

Kotlin

hilt {
    enableTransformForLocalTests = true
}

enableTransformForLocalTests에 관한 자세한 내용은 Hilt 문서를 참고하세요.

테스트 기능

Hilt가 테스트에 사용할 준비가 되면 몇 가지 기능을 사용하여 테스트 프로세스를 맞춤설정할 수 있습니다.

테스트에 유형 삽입

유형을 테스트에 삽입하려면 필드 삽입에 @Inject를 사용합니다. Hilt에 @Inject 필드를 채우도록 알리려면 hiltRule.inject()를 호출합니다.

계측 테스트의 다음 예를 참조하세요.

Kotlin

@HiltAndroidTest
class SettingsActivityTest {

  @get:Rule
  var hiltRule = HiltAndroidRule(this)

  @Inject
  lateinit var analyticsAdapter: AnalyticsAdapter

  @Before
  fun init() {
    hiltRule.inject()
  }

  @Test
  fun `happy path`() {
    // Can already use analyticsAdapter here.
  }
}

Java

@HiltAndroidTest
public final class SettingsActivityTest {

  @Rule public HiltAndroidRule hiltRule = new HiltAndroidRule(this);

  @Inject AnalyticsAdapter analyticsAdapter;

  @Before
  public void init() {
    hiltRule.inject();
  }

  @Test
  public void happyPath() {
    // Can already use analyticsAdapter here.
  }
}

결합 대체

종속 항목의 가짜 또는 모의 인스턴스를 삽입해야 한다면 프로덕션 코드에서 사용한 결합을 사용하지 말고 대신 다른 결합을 사용하도록 Hilt에 알려야 합니다. 결합을 대체하려면 결합이 포함된 모듈을, 테스트에 사용하려는 결합이 포함된 테스트 모듈로 대체해야 합니다.

예를 들어 프로덕션 코드에서 다음과 같이 AnalyticsService의 결합을 선언한다고 가정해 보겠습니다.

Kotlin

@Module
@InstallIn(SingletonComponent::class)
abstract class AnalyticsModule {

  @Singleton
  @Binds
  abstract fun bindAnalyticsService(
    analyticsServiceImpl: AnalyticsServiceImpl
  ): AnalyticsService
}

Java

@Module
@InstallIn(SingletonComponent.class)
public abstract class AnalyticsModule {

  @Singleton
  @Binds
  public abstract AnalyticsService bindAnalyticsService(
    AnalyticsServiceImpl analyticsServiceImpl
  );
}

테스트에서 AnalyticsService 결합을 대체하려면 가짜 종속 항목이 있는 test 또는 androidTest 폴더에 새 Hilt 모듈을 만들고 @TestInstallIn 주석을 답니다. 이 폴더의 모든 테스트에는 대신 가짜 종속 항목이 삽입됩니다.

Kotlin

@Module
@TestInstallIn(
    components = [SingletonComponent::class],
    replaces = [AnalyticsModule::class]
)
abstract class FakeAnalyticsModule {

  @Singleton
  @Binds
  abstract fun bindAnalyticsService(
    fakeAnalyticsService: FakeAnalyticsService
  ): AnalyticsService
}

Java

@Module
@TestInstallIn(
    components = SingletonComponent.class,
    replaces = AnalyticsModule.class
)
public abstract class FakeAnalyticsModule {

  @Singleton
  @Binds
  public abstract AnalyticsService bindAnalyticsService(
    FakeAnalyticsService fakeAnalyticsService
  );
}

단일 테스트에서 결합 교체

모든 테스트 대신 단일 테스트에서 결합을 바꾸려면 @UninstallModules 주석을 사용하여 테스트에서 Hilt 모듈을 제거하고 테스트 내에 새 테스트 모듈을 만듭니다.

이전 버전의 AnalyticsService 예에 따라 먼저 테스트 클래스의 @UninstallModules 주석을 사용하여 프로덕션 모듈을 무시하도록 Hilt에 지시합니다.

Kotlin

@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest { ... }

Java

@UninstallModules(AnalyticsModule.class)
@HiltAndroidTest
public final class SettingsActivityTest { ... }

다음으로, 결합을 대체해야 합니다. 다음과 같이 테스트 클래스 내에 테스트 결합을 정의하는 새 모듈을 만듭니다.

Kotlin

@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest {

  @Module
  @InstallIn(SingletonComponent::class)
  abstract class TestModule {

    @Singleton
    @Binds
    abstract fun bindAnalyticsService(
      fakeAnalyticsService: FakeAnalyticsService
    ): AnalyticsService
  }

  ...
}

Java

@UninstallModules(AnalyticsModule.class)
@HiltAndroidTest
public final class SettingsActivityTest {

  @Module
  @InstallIn(SingletonComponent.class)
  public abstract class TestModule {

    @Singleton
    @Binds
    public abstract AnalyticsService bindAnalyticsService(
      FakeAnalyticsService fakeAnalyticsService
    );
  }
  ...
}

이는 단일 테스트 클래스의 결합만 대체합니다. 모든 테스트 클래스의 결합을 바꾸려면 위 섹션의 @TestInstallIn 주석을 사용하세요. 또는 Robolectric 테스트의 경우 test 모듈에, 계측 테스트의 경우 androidTest 모듈에 테스트 결합을 배치할 수 있습니다. 가능하면 @TestInstallIn을 사용하는 것이 좋습니다.

새 값 결합

@BindValue 주석을 사용하면 테스트의 필드를 Hilt 종속 항목 그래프에 쉽게 결합할 수 있습니다. 필드에 @BindValue로 주석을 지정하면 이 필드에 대해 존재하는 한정자와 함께 선언된 필드 유형 아래에서 결합됩니다.

AnalyticsService 예에서 다음과 같이 @BindValue를 사용하여 AnalyticsService를 가짜로 대체할 수 있습니다.

Kotlin

@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest {

  @BindValue @JvmField
  val analyticsService: AnalyticsService = FakeAnalyticsService()

  ...
}

Java

@UninstallModules(AnalyticsModule.class)
@HiltAndroidTest
class SettingsActivityTest {

  @BindValue AnalyticsService analyticsService = FakeAnalyticsService();

  ...
}

이렇게 하면 결합을 대체하고 테스트에서 결합을 참조하는 작업이 동시에 가능하므로 두 작업을 간단하게 실행할 수 있습니다.

@BindValue는 한정자 및 기타 테스트 주석과 함께 작동합니다. 예를 들어 Mockito와 같은 테스트 라이브러리를 사용한다면 다음과 같이 Robolectric 테스트에서 사용할 수 있습니다.

Kotlin

...
class SettingsActivityTest {
  ...

  @BindValue @ExampleQualifier @Mock
  lateinit var qualifiedVariable: ExampleCustomType

  // Robolectric tests here
}

Java

...
class SettingsActivityTest {
  ...
  @BindValue @ExampleQualifier @Mock ExampleCustomType qualifiedVariable;

  // Robolectric tests here
}

다중 결합을 추가해야 하면 @BindValue 대신 @BindValueIntoSet@BindValueIntoMap 주석을 사용할 수 있습니다. 또한 @BindValueIntoMap에서는 맵 키 주석으로 필드에 주석을 지정해야 합니다.

특수한 사례

Hilt는 비표준 사용 사례를 지원하는 기능도 제공합니다.

테스트용 맞춤 애플리케이션

테스트 애플리케이션이 다른 애플리케이션을 확장해야 하므로 HiltTestApplication을 사용할 수 없다면 새 클래스 또는 인터페이스에 @CustomTestApplication으로 주석을 지정하여, 생성된 Hilt 애플리케이션이 확장하도록 하려는 기본 클래스의 값을 전달합니다.

@CustomTestApplication은 매개변수로 전달한 애플리케이션을 확장하는 Hilt로 테스트할 준비가 된 Application 클래스를 생성합니다.

Kotlin

@CustomTestApplication(BaseApplication::class)
interface HiltTestApplication

Java

@CustomTestApplication(BaseApplication.class)
interface HiltTestApplication { }

이 예에서 Hilt는 BaseApplication 클래스를 확장하는 HiltTestApplication_Application이라는 Application을 생성합니다. 일반적으로 생성된 애플리케이션의 이름은 _Application이 추가된 주석이 달린 클래스의 이름입니다. 테스트 애플리케이션에 설명된 대로 계측 테스트 또는 Robolectric 테스트에서 실행되도록 생성된 Hilt 테스트 애플리케이션을 설정해야 합니다.

계측 테스트의 여러 TestRule 객체

테스트에 다른 TestRule 객체가 있다면 모든 규칙이 함께 작동하도록 하는 여러 가지 방법이 있습니다.

다음과 같이 규칙을 함께 래핑할 수 있습니다.

Kotlin

@HiltAndroidTest
class SettingsActivityTest {

  @get:Rule
  var rule = RuleChain.outerRule(HiltAndroidRule(this)).
        around(SettingsActivityTestRule(...))

  // UI tests here.
}

Java

@HiltAndroidTest
public final class SettingsActivityTest {

  @Rule public RuleChain rule = RuleChain.outerRule(new HiltAndroidRule(this))
        .around(new SettingsActivityTestRule(...));

  // UI tests here.
}

또는 HiltAndroidRule이 먼저 실행되는 한 동일한 수준에서 두 규칙을 모두 사용할 수 있습니다. @Rule 주석의 order 속성을 사용하여 실행 순서를 지정합니다. 이 기능은 JUnit 버전 4.13 이상에서만 작동합니다.

Kotlin

@HiltAndroidTest
class SettingsActivityTest {

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

  @get:Rule(order = 1)
  var settingsActivityTestRule = SettingsActivityTestRule(...)

  // UI tests here.
}

Java

@HiltAndroidTest
public final class SettingsActivityTest {

  @Rule(order = 0)
  public HiltAndroidRule hiltRule = new HiltAndroidRule(this);

  @Rule(order = 1)
  public SettingsActivityTestRule settingsActivityTestRule = new SettingsActivityTestRule(...);

  // UI tests here.
}

launchFragmentInContainer

Hilt와 함께 androidx.fragment:fragment-testing 라이브러리의 launchFragmentInContainer를 사용할 수 없습니다. 이 코드는 @AndroidEntryPoint로 주석이 지정되지 않은 활동에 의존하기 때문입니다.

대신 architecture-samples GitHub 저장소의 launchFragmentInHiltContainer 코드를 사용하세요.

싱글톤 구성요소가 제공되기 전에 진입점 사용

@EarlyEntryPoint 주석은 Hilt 테스트에서 싱글톤 구성요소가 제공되기 전에 Hilt 진입점을 만들어야 하는 경우 이스케이프 해치를 제공합니다.

@EarlyEntryPoint에 관한 자세한 내용은 Hilt 문서를 참고하세요.