Tag: mockito

用Java + Mockito嘲笑Kotlin方法

所以我将一个小的Java代码库迁移到Kotlin只是为了好玩,而且我已经迁移了这个Java类: public class Inputs { private String engineURL; private Map<String, String> parameters; public Inputs(String engineURL, Map<String, String> parameters) { this.engineURL = engineURL; this.parameters = parameters; } public String getEngineURL() { return engineURL; } public String getParameter(String key) { return parameters.get(key); } } 到这Kotlin表示: open class Inputs (val engineURL: String, private val parameters: Map<String, String>) { […]

Mockito嘲笑像间谍:Kotlin

我正在研究一个kotlin jar,我试图模拟一个函数的输入 class MyService fun serviceFunction(input: ClassFromAnotherLibrary): Output { val foo = input.memberFunction() 只是碰巧, memberFunction已经通过包级扩展函数被添加到我的包中的那个类 fun ClassFromAnotherLibrary.memberFunction() : Foo { val mapper = jacksonObjectMapper() return mapper.readValue(this.serializedFoo, Foo::class.java) } 现在我想写一个serviceFunction的测试,但我想嘲笑memberFunction调用(我有单独的测试)。 所以在我的Mockito JUnit测试中,我做了以下 val service = Service() val mockClassFromAnotherLibrary = mock<ClassFromAnotherLibrary>() val mockFoo = mock<Foo>() whenever(mockClassFromAnotherLibrary.memberFunction()) .thenReturn(mockFoo) service.serviceFunction(mockClassFromAnotherLibrary) 我期望memberFunction的实际实现永远不会被调用,而且我的模拟会拦截任何尝试调用它,而是返回我的mockFoo 。 实际上 , whenever设置模拟出调用底层函数的方法时,当mapper尝试读取serializedFoo (当然是null)时,会导致NullPointerException 。 我的问题是: 为什么地球上真正的memberFunction被执行? […]

嘲笑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单元测试 ,我们可以测试函数变量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) 或者函数参数的内联(如上)对于单元测试是不利的,因此应该避免?

嘲笑与Kotlin和Mockito的接口

我不太了解Kotlin的界面模拟。 我不确定这是Kotlin特定还是适用于Java。 到目前为止,我只在Kotlin尝试过。 我有一个接口模拟类: val mockObj = mock(MyClass::class.java, withSettings().extraInterfaces(IMyInterface::class.java) 现在,这在IntelliJ中产生以下问题: `when`(mockObj.someMethod()).thenReturn(0.1) someMethod()将是红色的 (我认为这取决于配色方案,但是你得到的想法 – 一个问题: 未解决的参考 ) 为了解决这个问题,我只是简单的把它包括在它之前: val mockInterface = mockObj as IMyInterface 模拟接口将被灰色下划线,因为它从来没有使用过 。 我仍然使用mockObj的when 然而,这使得红色消失 我不知道为什么这个作品…有人可以解释这个理论吗? 为了摆脱灰色的下划线,我然后删除val mockInterface = 所以我的魔术线减少到mockObj as IMyInterface

Mocktito ArgumentCaptor Kotlin lambda与参数

我试图在Kotlin上测试这个: verify(myInterface).doSomething(argumentCaptor.capture()) capture.value.invoke(0L) 哪里做什么: doSomething((Long) -> Unit) 我怎样才能为此创建一个ArgumentCaptor? 现在我正在做这个 inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)!! val captor = argumentCaptor<(Long) -> Unit>() verify(mainApiInterface!!).downloadUserProfilePicture(captor.capture()) captor.value.invoke(0L) 但是我得到java.lang.IllegalStateException:captor.capture()不能为空 我也尝试整合mockito-kotlin,但我得到一个PowerMockito错误: 在org.mockito.internal.MockitoCore的类层次结构中找不到名为“reported”的实例字段。

使用Kotlin-Mokito库时java.lang.reflect.InvocationTargetException

任何()从Kotlin Mockito库崩溃与下面的代码 测试类 import com.nhaarman.mockito_kotlin.any import com.nhaarman.mockito_kotlin.verify import org.junit.Before import org.junit.Test import org.mockito.Mock import org.mockito.MockitoAnnotations class SimpleClassTest { lateinit var simpleObject: SimpleClass @Mock lateinit var injectedObject: InjectedClass @Before fun setUp() { MockitoAnnotations.initMocks(this) } @Test fun testSimpleFunction() { simpleObject = SimpleClass(injectedObject) simpleObject.simpleFunction() verify(injectedObject).settingDependentObject(any()) } } 源类 import com.squareup.okhttp.Protocol import com.squareup.okhttp.Request import com.squareup.okhttp.Response class SimpleClass(val injectedClass: InjectedClass) […]

如何模拟Kotlin单身物件?

给定一个Kotlin单身对象和一个叫它的方法的乐趣 object SomeObject { fun someFun() {} } fun callerFun() { SomeObject.someFun() } 有没有办法模拟调用SomeObject.someFun() ?

嘲笑Kotlin的扩展功能

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

与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 = […]