Tag: unit testing

一个简单的kotlin类与mockito测试引起MissingMethodInvocationException

我开始学习Kotlin和Mockito,所以我编写了一个简单的模块来测试它。 AccountData_K.kt: open class AccountData_K { var isLogin: Boolean = false var userName: String? = null fun changeLogin() : Boolean { return !isLogin } } AccountDataMockTest_K.kt: class AccountDataMockTest_K { @Mock val accountData = AccountData_K() @Before fun setupAccountData() { MockitoAnnotations.initMocks(this) } @Test fun testNotNull() { assertNotNull(accountData) } @Test fun testIsLogin() { val result = accountData.changeLogin() assertEquals(result, […]

doReturn不作为代理,但调用我的方法

我正在实施一些unit testing在我的应用程序,我有一个依赖于依赖于Application上下文。 该依赖返回一个Item列表,但我想嘲笑该逻辑返回任何我想要的。 我已经知道的区别: a) when(foo.method1()).return(bar) b) doReturn(bar).when(foo).method1() (b)不应调用该方法。 现在我有一个在ItemHelper.kt调用的方法: fun retrieveItems(): MutableList { val boxStore = BoxStore.getInstance().getBoxFor(Item::class.java) return boxStore.all } 而.getInstance()依赖于Application 。 既然我想嘲笑它,这是我的考验: class ItemHelperTests { @JvmField @Rule var mockitoRule = MockitoJUnit.rule()!! @Mock private lateinit var itemHelper: ItemHelper @Test fun itemsNumber_Test() { Mockito.doReturn(ArrayList()).`when`(itemHelper).retrieveItems() System.out.println(“this line is unreachable”) } } 但是结束调用我的代码: java.lang.Exception: Please init BoxStore.boxStore […]

可以在Kotlin中编写函数(即在顶层)。 我可以unit testing那些顶级?

如果我在顶层编写代码(在课堂之外)。 我可以编写此代码的JUnit测试,而无需为此测试目的创建类?

Android测试运行时不需要启动模拟器(选择设备对话框不会出现) – 空的测试套件

我正在努力运行在androidTest文件夹下的测试 – 目前即使当我不启动模拟器没有它的测试运行,没有对话框强迫我选择我必须运行测试的设备。 它导致问题,当我运行测试错误出现在控制台中: 处理完成退出代码1类未find:“com.someapp.shoppinglistapp.ShoppingListDaoTest”空的测试套件。 只有当我手动编辑配置这个单独的方法选择“部署目标选项”,并选择“打开选择部署目标对话框”它运行良好,我选择模拟器和测试运行。 但这意味着我将不得不手动为我的androidTest包中的每个测试方法。 究竟发生了什么? 我甚至试过这个全新的项目,配置成Android Studio 3.0.1中的Kotlin项目。 你可以自己尝试。

Androidunit testing没有被嘲笑

我遵循本指南https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support,但我坚持这个错误: junit.framework.AssertionFailedError: Exception in constructor: testSaveJson (java.lang.RuntimeException: Method put in org.json.JSONObject not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details. 我修改了Gradle构建像指南说,​​但它没有区别 testOptions { unitTests.returnDefaultValues = true }

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生成代码覆盖,但它也没有显示代码覆盖率。

Instrumentation测试将作为Android Studio 3(空测试套件)上的Local Unit测试运行,

我在Android Studio 3.0.1和Kotlin中运行Instrumentation Tests时遇到了一些问题(我之前的Android Studios和Java没有这样的问题)。 我在androidTest包中创建了这个(非常简单的)类[ 引用 ],如下所示: @RunWith(AndroidJUnit4::class) @LargeTest class DiscoverActivityTest { @JvmField @Rule val mDiscoverActivityTestRule = ActivityTestRule(DiscoverActivity::class.java) @Test fun onViewLoadedShowDiscoverFragment() { onView(withId(R.id.discoverFragmentView)).check(matches(isDisplayed())) } } 当我尝试运行测试时,有两个问题。 首先,它希望在控制台中运行测试,而无需打开模拟器或将apk包部署到设备(因为这只是一个正常的本地unit testing)。 另外,我也得到这个错误信息: 未find类:“com.site.application.discover.DiscoverActivityTest”空的测试套件。 我想在“ Edit Configuration选项(比如这个解决方案的例子)中强制使用检测工具,但是在新的Android Studio 3.0.1中没有这样的选项(比如2.3版本) 更新: 这里是我的gradle文件内容: apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ android { compileSdkVersion 27 defaultConfig { applicationId “com.site.application” […]

Android Studio + Spek集成

我正在尝试将Spek测试框架添加到我的Android Studio项目中。 按照说明在这里 ,我结束了以下添加到我的模块build.gradle : testCompile ‘org.jetbrains.spek:spek-api:1.1.5’ testCompile ‘junit:junit:4.12’ testCompile “org.junit.platform:junit-platform-runner:1.0.0” testRuntimeOnly ‘org.jetbrains.spek:spek-junit-platform-engine:1.1.5’ 然后我用@RunWith(JUnitPlatform::class)注释了我的测试。 但是,当我尝试运行测试,我得到: org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath 任何想法我错过了什么?

运行Spek测试显示错误“空测试套件”

与Kotlin熟悉了一下,我想介绍一下另一个Android-Java项目,作为测试的第一步。 我决定直接与Spek开始。 我添加了以下依赖关系来构建要测试的模块的渐变: testCompile ‘junit:junit:4.12’ testCompile “org.jetbrains.kotlin:kotlin-stdlib:1.0.2” testCompile “org.jetbrains.kotlin:kotlin-test-junit:1.0.2” testCompile “org.jetbrains.spek:spek:1.0.25” 其中我使用了SimpleTest类的git仓库的spek-samples: import org.jetbrains.spek.api.Spek import kotlin.test.assertEquals class SampleCalculator { fun sum(x: Int, y: Int) = x + y fun subtract(x: Int, y: Int) = x – y } class SimpleTest : Spek({ describe(“a calculator”) { val calculator = SampleCalculator() it(“should return the result of adding […]

当使用kotlin协同程序时,如何对一个调用暂停function的函数进行unit testing?

我有这样的课 class SomeClass { fun someFun() { // … Some synchronous code async { suspendfun() } } private suspend fun suspendFun() { dependency.otherFun().await() // … other code } } 我想unit testingsomeFun()所以我写了一个unit testing,看起来像这样: @Test fun testSomeFun() { runBlocking { someClass.someFun() } // … verifies & asserts } 但是,这似乎不工作,因为runBlocking实际上并没有阻止执行,直到runBlocking内的一切完成。 如果我直接在runBlocking里面测试suspendFun()它可以像预期的那样工作,但是我想能够一起测试someFun() 。 任何线索如何测试同步和异步代码的function?