Tag: mockito

什么是Java相当于Kotlin的函数types?

我有Kotlin代码和Java测试代码。 由于Kotlin和Mockito不是最好的朋友,所以我没有将测试代码迁移到Kotlin 。 在Kotlin我有块types的方法。 例如: open fun getProductInfo(resultListener: (List)->Unit, errorListener: (Throwable)->Unit) { … } 现在我想在Java测试中存储这个方法。 什么是types的Java相当于什么? 换句话说,我应该写下面的代码: doAnswer(invocation -> { ??? resultListener = (???) invocation.getArguments()[0]; // call back resultListener return null; }).when(api).getProductInfo(any(), any());

测试环境配置:Android + JUnit 5 + Mockito + Spek + Kotlin

我很难配置基于JUnit Jupiter(5)的测试环境。 我有两个不同的错误: WARNING: TestEngine with ID ‘spek’ failed to discover tests org.junit.platform.commons.util.PreconditionViolationException: Could not load class with name… Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61)… 配置如下。 主build.gradle : apply plugin: ‘org.junit.platform.gradle.plugin’ buildscript { ext.kotlin_version = ‘1.1.4-3’ repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.0.0-beta5’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” classpath “org.junit.platform:junit-platform-gradle-plugin:1.0.0” classpath “de.mannodermaus.gradle.plugins:android-junit5:1.0.0” } } […]

SpringBoot ArrayIndexOutOfBoundsException MethodParameter.getGenericParameterType

我正在使用spring引导,我试图建立一个测试场景,我调用一个bean,调用一个模拟JPARepository使用mockito。 这是相关的代码: 存储库 @Repository interface FooRepository : JpaRepository 服务 @Service class FooService( private val FooRepository: FooRepository ) { @Transactional fun call(input: String) = FooRepository.saveAndFlush( Foo(input) ) } 和测试 @SpringBootTest @RunWith(SpringRunner::class) @Import(FooServiceTest.MockFooRepositoryConfiguration::class) class FooServiceTest { @Autowired private lateinit var fooService: FooService @Autowired private lateinit var fooRepository: FooRepository @Test fun test1() { FooService.call(“xxx”) Mockito.verify(FooRepository, Mockito.times(1)).saveAndFlush(“xxx”) } @Configuration […]

Mockitounit testing:在一个类的所有方法调用中,返回“true”

我在kotlin SmsHandler有一个类,使用一个方法determineFiltersPass调用另一个类SmsSendingFilters : class SmsHandler(val filterPredicates: SmsSendingFilters) { fun determineFiltersPass(sms: SmsDto): Boolean = with(sms.filters) { var pass = true for (filter in FiltersDto::class.memberProperties) pass = when (FilterType.valueOf(filter.name.toUpperCase())) { UNIQUE -> if (filter.get(sms.filters) != null) { val unique = filter.get(sms.filters) as Boolean pass && if (unique) filterPredicates.isUnique().test(sms) else true } else pass && true RECENT -> […]

如何使用Mockito / PowerMockito来模拟Kotlin的对象?

我有以下class级: object CharacterDAO : GenericDAO(Character::class.java, “id”) { } 我想嘲笑使用Mockito / PowerMockito。 有什么建议么?

在kotlinunit testing中获得lambda捕获器的调用计数

我有这个小小的和平代码,我想在我的主持人(MVP) fun load(id: String) { storage.load(id, { result -> this.result = result view?.notifyLoaded(result) }, { string -> … }) } 。 我在我的unit testing用例中storage.load(String, (Result)->Unit, (String)->Unit) 。 `when`(storage.load(eq(testResult.id), any Unit>(), any Unit>())).thenAnswer({ invocation -> (invocation.getArgument(1) as? (Result) -> Unit)?.invoke(testResult) }) 现在我的测试案例看起来像这样 @Test fun testLoading() { /* Given */ … /* When */ presenterUnderTest.load(testResult.id) /* Then */ […]

如何为Kotlin和Android设置Mockito

我想使用Mockito进行unit testing,所以我将Mockito库添加到我的gradle依赖项中。 testImplementation ‘junit:junit:4.12’ testCompile ‘org.mockito:mockito-core:2.12.0’ 但是,我仍然不能使用任何Mockito注释。 /androidTest/ExampleTest.kt @RunWith(MockitoJUnitRunner::class) // Unresolved reference MockitoJUnitRunner @Mock // Unresolved reference Mock 我错过了什么?

kotlin测试whith mockito:比较失败

我试图用简单的测试主持人来覆盖,看起来像下一个: class Presenter{ fun getData(params:SomeParams) { usecase.execute(getObservable, params) } private fun getObservable() = object :DisposableObserver{ override fun onComplete() {} override fun onNext(t:SomeData) {} override fun onError(e: Throwable) {} } } 这是我的简单测试: @Test fun getContacts() { presenter.getData() var observer = Mockito.mock(DisposableObserver::class.java) as DisposableObserver verify(useCase).execute(observer, someParams) } 和最后一行后我得到下一个错误: Argument(s) are different! Wanted: useCase.execute( com.test.PresenterTest$getObservable$o$1@579d011c, kotlin.Unit ); -> […]

使用kotlin lambda回调的unit testing场景

假设我们有以下function来测试 fun loadData(dataId: Long, completion: (JsonElement?, Exception?) -> Unit { underlayingApi.post(url = “some/rest/url”, completion = { rawResult, exception -> val processedResult = processJson(rawResult) completion(processedResult, exception) }) } 我很清楚如何模拟,注入,存根和validation对underlayingApi的调用。 我的问题是:代码如何看起来像validation通过completion(processedResult, exception)返回的结果completion(processedResult, exception) ?

在Kotlin中可以使用Mockito吗?

我面临的问题是Matchers.anyObject()返回null 。 当用于模拟只接受不可为空的types的方法时,会导致抛出“不应为空”的exception。 `when`(mockedBackend.login(anyObject())).thenAnswer { invocationOnMock -> someResponse } 嘲笑方法: public open fun login(userCredentials: UserCredentials): Response