草庐IT

unit-testing - Android Studio + Spek 集成

我正在尝试将Spek测试框架添加到我的AndroidStudio项目中。按照说明Here,我最终将以下内容添加到我的模块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(JUnitPla

unit-testing - Kotlin 中的测试无法访问 protected 方法

我要考B类:classB:A{overridefuninit(){//doworkhere}}classA{protectedfuninit(){}//willbecalledbyinternallogic}在Java中调用没有问题:b.init()在测试方法中(测试类与测试对象在同一个包中),但是在Kotlin编译器报错:Cannotaccess'init':itisprotectedin'B'@Testfun`checksinit`(){valb=B()b.init()//assertworkdone}为什么它不起作用?如何解决这个问题(我想避免公开方法)?

function - 如何使用 Kotlin 将函数输出转换为 Unit

我在Kotlin中应该返回Unit的函数遇到问题,但由于使用了另一个返回bool值的函数,存在类型不匹配。这是一个人为的例子:funprintAndReturnTrue(bar:Int):Boolean{println(bar)returntrue}funfoo(bar:Int):Unit=when(bar){0->println("0")else->printAndReturnTrue(bar)}在这里,我实际上并不关心printAndReturnTrue返回bool值的事实。我只想foo执行副作用操作。但是编译器会警告类型不匹配:我的else应该返回一个Unit值。有没有一种将值转

unit-testing - 在 kotlin 中模拟同伴对象函数

我正在使用PowerMock和Roboelectric,并希望模拟一个类的伴随对象函数。当我这样做时,我得到一个错误:org.mockito.exceptions.misusing.MissingMethodInvocationException:when()requiresanargumentwhichhastobe'amethodcallonamock'.Forexample:when(mock.getArticles()).thenReturn(articles);我所拥有的基本上是这样的:openclassMockableClassprivateconstructor(cont

unit-testing - 使用 PowerMock 在 Kotlin 中模拟包级函数

我在Kotlin中有一个包含一些包级函数的文件。//Logger.ktfuninfo(tag:String,message:String){...}funerror{....}我正在测试一个调用这个kotlin文件函数的类的函数,我想模拟它们。我知道包级函数就像Java中的静态方法,所以我一直在考虑使用PowerMock。//MyClass:ClassthatcallsLogger.ktfunctionsclassMyClass{funmyFunction(){info("TAG","Helloworld!")}}有什么想法吗? 最佳答案

kotlin - 类型不匹配推断类型为 Unit 但应为 Void

一个带有字符串和监听器(类似于swift中的闭包)参数的kotlin方法。funtestA(str:String,listner:(lstr:String)->Void){}这样调用它。testA("hello"){lstr->print(lstr)}Error:TypemismatchinferredtypeisUnitbutVoidwasexpected什么是单位??闭包的返回类型是Void。阅读很多其他问题,但可以通过这种简单的方法找到这里发生的事情。 最佳答案 根据Kotlin文档,单元类型对应于Java中的void类型。所

unit-testing - 如何测试在 Kotlin 中调用顶级函数的代码?

我对Kotlin很陌生。我有一个调用顶级函数(进行http调用)的类。我正在尝试为我的类(class)编写单元测试,而无需将其发布到网络上。有没有办法模拟/powermock/拦截从我的类(class)到Kotlin顶级函数的调用?classMyClass{funsomeMethod(){//dosomestuff"http://somedomain.com/some-rest/action".httpGet(asList("someKey"to"someValue")).responseString{(request,response,result)->//someprocessin

kotlin - () -> Unit 和 (Unit) -> Unit 类型有什么区别?

我有以下功能:funprocess(t:T,call:(U)->Unit,map:(T)->U)=call(map(t))funprocessEmpty(t:T,call:()->Unit)=process(t,call,{})//error但是processEmpty没有编译。错误消息是Typemismatch:inferredtypeis()->kotlin.Unitbut(kotlin.Unit)->kotlin.Unitisexpected。但是如果我将此功能更改为funprocessEmpty2(t:T,call:(Unit)->Unit)=process(t,call,{}

unit-testing - 使用 kotlin 协程时,如何对调用挂起函数的函数进行单元测试?

我有这样的课classSomeClass{funsomeFun(){//...Somesynchronouscodeasync{suspendfun()}}privatesuspendfunsuspendFun(){dependency.otherFun().await()//...othercode}}我想对someFun()进行单元测试,所以我编写了一个如下所示的单元测试:@TestfuntestSomeFun(){runBlocking{someClass.someFun()}//...verifies&asserts}但这似乎不起作用,因为在runBlocking中的所有内容完

unit-testing - Kotlin:使内部函数对单元测试可见

如果测试与生产代码位于不同的模块中(这很常见),使内部函数对测试可见的最佳方法是什么?在Java中,我会将生产代码和测试放在同一个包中,并将待测试方法包设为私有(private)(另外,如果唯一的话,添加@VisibleForTest注释让它包私有(private)而不是私有(private)的原因是测试)。不幸的是,Kotlin没有包私有(private)的概念。 最佳答案 用internal访问修饰符标记的类和方法将在Kotlin、Gradle和Intellij的当前版本中工作,以便从测试类访问这些方法。这些工具将主源路径和测试