Tag: 单元测试

Android工作室代码覆盖不显示任何Kotlin类

我有2个基于Kotlin的AS项目。 一个正常工作,另一个没有。 两者具有相同的结构和gradle配置。 我使用Kotlin 1.1.4-2和AS 3.0b2。 问题是,当我运行代码覆盖时,我测试的类甚至不在覆盖报告中显示。 我看到的唯一的东西是R.java和BuildConfig.java,但没有找到我的源文件。 所有源文件都是基于Kotlin的。 我正在使用Spek进行单元测试。 与我的其他功能项目相同的设置和版本。 我尝试使用JaCoCo和Jetbrains代码覆盖率,都没有工作。 我很抱歉,我还无法嵌入图片。 这是问题: 覆盖率报告 这是我的项目结构: 文件结构 这里是我的gradle android块: compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { minSdkVersion 16 targetSdkVersion 26 versionCode 1 versionName libraryVersion } buildTypes { debug { testCoverageEnabled = true } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } 和我的依赖: implementation fileTree(dir: 'libs', […]

在Java单元测试中模拟Kotlin类

我有Mockito的这个Java测试: public class PersistentNodeDeserializerTests { @Test public void userInfoPersistentNodeDeserializer() { PersistentNode node = mock(PersistentNode.class); when(node.stringChild("username")).thenReturn("cliff12"); //more stuff } } PersistentNode是一个Kotlin类: open class PersistentNode(private val path: PersistentNodePath, val content: Any) { val stringPath: String get() = path.get() val key: String get() { val parts = stringPath.split("/"); return parts[parts.size – 1]; } val mapContent: Map<String, Any> get() […]

嘲笑Kotlin的最后一场

我是Mockito Kotlin的新手。 下面的代码有一个engine引起的错误,一个不被模拟的不可变字段。 我花了很多时间来解决这个问题。 我发现一种嘲笑的方式,直到我不明白,不满足。 将一个字段移到构造函数中。 class Car(val engine: Engine) { … } 改变为可变字段。 private var engine = Engine() 有没有其他的方法? class Car { private val engine = Engine() // here var state: String? = null fun move() { state = engine.state } } @RunWith(MockitoJUnitRunner::class) class CarTest { @Mock private lateinit var mockedEngine: Engine @InjectMocks private […]

写作单元在Kotlin测试,共享变量?

我正在尝试在Kotlin中创建一些功能测试,以使用Rest Assured库向购物车Java服务器发出请求。 因为我希望测试的行为程序,我希望我可以存储第一个A​​PI请求的结果,并将其传递到下一个单元测试。 即 createCartTest() – > cartId – > getCartForWebsiteTest(cartId) class CartTest : RestAssuredSupport { val port = 8080 val url = "http://localhost:" val cartId = null /** * Create a cart object */ @Test fun createCartTest() { given(). now(). body("websiteId=1"). contentType(ContentType.URLENC). post(url + port + "/orders/cart/create.do"). then(). statusCode(200). body("summary.numItems", equalTo(0)). body("summary.visibleNumItems", equalTo(0)). body("summary.cartId", […]

Kotlin匿名函数参数单元测试

根据函数参数和对象的Kotlin单元测试 ,我们可以测试函数变量funcParam ,因为它是一个对象函数变量。 但是,如果代码是使用匿名/内联函数参数编写的(这是一个非常好的Kotlin功能,可以让我们消除不必要的临时变量)… class MyClass1(val myObject: MyObject, val myObject2: MyObject2) { fun myFunctionOne() { myObject.functionWithFuncParam{ num: Int -> // Do something to be tested myObject2.println(num) } } } class MyObject () { fun functionWithFuncParam(funcParam: (Int) -> Unit) { funcParam(32) } } 如何编写测试这部分代码的单元测试? num: Int -> // Do something to be tested myObject2.println(num) 或者函数参数的内联(如上)对于单元测试是不利的,因此应该避免?

未解决的参考:DaggerTestComponent(Kotlin用匕首测试)

当我们使用Dagger和Kotlin时,我们需要在build.gradle依赖中使用以下内容 kapt 'com.google.dagger:dagger-compiler:2.0' compile 'com.google.dagger:dagger:2.0' provided 'org.glassfish:javax.annotation:10.0-b28' 如http://www.beyondtechnicallycorrect.com/2015/12/30/android-kotlin-dagger/所述 当我尝试使用Dagger进行测试时,按照https://labs.ribot.co.uk/fast-and-reliable-ui-tests-on-android-17c261b8220c#.o3efc5knx生成DaggerTestComponent.builder()甚至https://medium.com/@fabioCollini/android-testing-using-dagger-2-mockito-and-a-custom-junit-rule-c8487ed01b56#.hxtytfns3,Kotlin语言,但有以下错误 Error:(14, 25) Unresolved reference: DaggerTestComponent 我发现https://stackoverflow.com/a/36231516/3286489解释如何获得DaggerTestComponent生成,并尝试把下面的依赖。 androidTestApt 'com.google.dagger:dagger-compiler:2.0.1' 显然,我认为这是针对Java而不是Kotlin,所以问题依然存在。 它有一个Kotlin版本吗? 如何让我的Kotlin项目中生成的DaggerTestComponent ?

Kotlin – Maven不执行测试

所以我有一个我想测试的Kotlin应用程序。 我的测试( .kt )文件,在Eclipse中成功执行。 (测试本身是一个h2 mock jdbc测试)。 现在当运行mvn test -X它说: releases: [enabled => true, update => never] ] [DEBUG] (s) reportFormat = brief [DEBUG] (s) reportsDirectory = C:\Users\Ivar\workspace\anotherworkspace\newrestservice\complete\target\surefire-reports [DEBUG] (f) rerunFailingTestsCount = 0 [DEBUG] (f) reuseForks = true [DEBUG] (s) runOrder = filesystem [DEBUG] (f) shutdown = testset [DEBUG] (s) skip = false [DEBUG] (f) […]

用JMockit和Kotlin捕获对象

我已经使用了JMockit很久了,我非常喜欢它。 但是,我遇到了一个我似乎无法解决的问题。 查看下面的一些Kotlin测试代码片段,测试Kotlin生产代码。 @Injectable lateinit var experimentStorage: ExperimentStorage … val experimentCaptor = mutableListOf<Experiment>() object : Verifications() { init { experimentStorage.save(withCapture(experimentCaptor)) } } 当我运行我的测试时,我得到以下错误: java.lang.IllegalStateException:withCapture(experimentCaptor)不能为null 我100%确定我的生产代码正确地执行存储,因为当我替换下面的捕获时,我的测试成功: object : Verifications() { init { experimentStorage.save(withAny(experiment)) } } 有没有人有经验捕捉Kotlin与JMockit(1.28)的参数? 我究竟做错了什么? 我想它与init块有关,因为在Java中你将使用静态空间…

嘲笑Kotlin的扩展功能

如何在测试中使用Mockito或PowerMock来模拟Kotlin扩展功能? 由于它们是静态解析应该作为静态方法调用还是非静态?

从Android Studio运行Kotlin Koans测试

Windows 10,Android Studio 3.0。 所以我只是从master分支克隆Kotlin Koans回购,并试图用两种不同的方式运行测试: 1.使用IDE,当按下测试方法旁边的绿色箭头时,我看到: Process finished with exit code 1 Class not found: "i_introduction._0_Hello_World.N00StartKtTest"Empty test suite. 2.当我尝试使用终端,如Kotlin Koans自述: gradlew test –tests i_* 它给了我一个输出: :compileKotlin Using kotlin incremental compilation Caught an exception trying to connect to Kotlin Daemon java.lang.ClassNotFoundException: com.sun.tools.javac.util.Context 我已经看到了很多的建议,可能在jdk文件夹中缺少tools.jar,我正在使用内部的Android Studio jdk,并在这里介绍。 我也尝试禁用gradle deamons – 结果仍然是一样的。