Tag: 单元测试

如何使用kotlin-test编写Kotlin单元测试?

我正在尝试使用Kotlin测试我的代码。 当试图导入kotlin.test包时,什么都不显示。 我必须先在某处添加依赖项吗? 我怎样才能运行我的单元测试?

在Kotlin中测试预期的例外

在Java中,程序员可以为JUnit测试用例指定预期的异常,例如: @Test(expected = ArithmeticException.class) public void omg() { int blackHole = 1 / 0; } 我如何在Kotlin做这个? 我已经尝试了两种语法变体,但都没有工作: import org.junit.Test as test // … test(expected = ArithmeticException) fun omg() Please specify constructor invocation; classifier 'ArithmeticException' does not have a companion object test(expected = ArithmeticException.class) fun omg() name expected ^ ^ expected ')'

与Kotlin的Mockito:AbstractMethodError

我正在努力使谷歌的测试实验室代码示例与kotlin一起工作。 但转换后,我想嘲笑kotlin接口我得到一个AbstractMethodError ,我无法解决。 的build.gradle: testCompile "org.hamcrest:hamcrest-all:1.3" testCompile "junit:junit:4.12" testCompile "org.mockito:mockito-core:2.8.47" testCompile "org.powermock:powermock-module-junit4:1.6.2" testCompile "org.powermock:powermock-api-mockito:1.6.2" NotesPresenterTest.kt package com.example.android.testing.notes.notes import org.mockito.Matchers.any /** * Unit tests for the implementation of [NotesPresenter] */ class NotesPresenterTest { @Mock private val mNotesView: NotesContract.View? = null @Before fun setupNotesPresenter() { MockitoAnnotations.initMocks(this) // Get a reference to the class under test mNotesPresenter = […]

Android Studio 3 / Kotlin代码覆盖

我的Android应用程序是多模块项目: include (android-app/kotlin-android)':application', (pure kotlin)':presentation', (pure kotlin)':domain', (android-library/kotin-android)':dataproviders' 我正在使用Junit / Mockito进行测试,并且只有为kotlin android模块生成代码覆盖的问题。 经过测试的行对于android studio是可见的。 在ui.viewmodel包中测试类: 但是,对于纯粹的kotlin (例如,域,演示)测试覆盖工作正常: 我正在使用Android Studio 3.0 Canary 8您可以在github上查看我的build.gradle文件: 的build.gradle dependencies.gradle application.build.gradle presentation.build.gradle Android应用程序模块中的示例测试: MostPopularViewModelTest 纯kotlin模块中的示例测试: MostPopularPresenterTest 有人可以帮我解决我的问题吗? 我尝试通过Jacoco生成代码覆盖,但它也没有显示代码覆盖率。