Kotlin + Mockito2:用于演示者测试的ClassNotFoundException

时间:2017-09-12 21:13:09

标签: android unit-testing mockito kotlin

我有一个基于CleanArchitecture-Android风格的Kotlin编写的示例应用程序。以下是该应用程序的配置:

呈现/的build.gradle

dependencies {
  implementation project(':domain')

  def presentationDependencies = rootProject.ext.presentationDependencies
  def commonDependencies = rootProject.ext.commonDependencies

  testCompile testingDependencies.junit
  testCompile testingDependencies.mockito // org.mockito:mockito-inline:2.10.0
}

呈现/ SRC /测试/ JAVA /包/ RepoPreferencesPresenterTest.kt

internal class RepoPreferencesPresenterTest : UnitTest() {
  // FIXME: Mocking fails
  @Mock private lateinit var mockRepositoriesUseCase: RepositoriesUseCase
  @Mock private lateinit var mockView: RepoPreferencesContract.View

  private lateinit var presenter: RepoPreferencesPresenter<RepoPreferencesContract.View>

  @Before
  fun setUp() {
    presenter = RepoPreferencesPresenter(mockRepositoriesUseCase)
    presenter.onAttach(mockView)
  }
}

当我尝试运行测试用例时,我每次都会遇到ClassNotFoundException。无法模拟演示者测试中域模块中存在的UseCase,除了该应用程序正常运行。

java.lang.NoClassDefFoundError: Lcom/sample/domain/usecases/RepositoriesUseCase;
......
Caused by: java.lang.ClassNotFoundException: com.sample.domain.usecases.RepositoriesUseCase
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

知道如何解决问题吗? (我也试过使用mock-maker-inline,但它不起作用)

1 个答案:

答案 0 :(得分:0)

通过手动指定presentation/build.gradle

中所有模块的类路径解决了该问题
sourceSets {
    main.java.srcDirs += 'src/main/java'
    main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
    main.java.srcDirs += '../data/src/main/java'
    main.java.srcDirs += '../entities/src/main/java'
    main.java.srcDirs += '../domain/src/main/java'
  }

设置:Gradle版本“3.0.0-beta6”,Android Studio 3.0-Beta 6.