Tag: mockito

与Robolectric的Mockito:“创建代理时发生ClassCastException”

在Robolectric中使用它时,我开始在Mockito中碰到一个奇怪的ClassCastException。 当我运行相同的测试不使用Robolectric转轮,一切顺利,没有例外抛出。 这里是堆栈跟踪: org.mockito.exceptions.base.MockitoException: ClassCastException occurred when creating the proxy. You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration) at com.compassrosetech.ccs.android.test.ObservableCacheDispatcherTest.setUp(ObservableCacheDispatcherTest.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at […]

如何验证使用mockito的kotlin可变参数函数

我在界面中有一个这种形式的kotlin函数: fun foo(bar: String, vararg baz: Pair<String, ByteArray>):Boolean 使用Mockito来模拟这个接口,我如何验证这个函数被调用没有对? 它不能使第二个匹配器关闭,因为那么Mockito抱怨​​说它需要两个匹配器。 使用任何any*()匹配器,包括anyVararg() ,由于输入而失败。

在Mockito类的模拟过程中投射参数

当我用下面的方法(在Kotlin中) val card: Card = mock<Card>()模拟类时val card: Card = mock<Card>()它会自动将所有参数(cardNumber,expMonth,expYear,CVC)初始化为String,因为它们保存为String。 然而,在我要测试的代码(Java)中,我通过以下方式初始化类卡 Card card = new Card(mView.getCreditCardNumber(), Integer.valueOf(mView.getMonth()), Integer.valueOf(mView.getYear()), mView.getCVV()); 我已经用以下方式嘲笑了这些价值 whenever(mView.creditCardNumber).then { "1234567890123456" } whenever(mView.month).then { "12" } whenever(mView.year).then { "2022" } whenever(mView.cvv).then { "123" } 所以区别在于中间的2个参数应该是整数,而我不能这样做。 我尝试了以下解决方案 whenever(mView.month).then { 12 } whenever(mView.year).then { 2022 } 但是我从Int到String得到CastException。 由于mView.month /年应该是字符串。 那么,有没有办法使这个强制转换,否则我不能够正确地测试代码的其余部分。 请帮忙,否则我疯了。 谢谢 :) !

与Kotlin测试异步失败的Mockito

我正在尝试为我的测试学习Mockito,但使用Kotlin时则更加复杂。 我想在我的演示者中测试一个函数,当调用者从存储库异步获取对象列表并从侦听器触发Success / onError时。 之后,主持人根据回调告诉视图做任何事情。 以下是测试代码: @Mock lateinit var view: CategoriesActivityContract.View @Mock lateinit var repository: CategoriesRepository @Before fun setUp() { MockitoAnnotations.initMocks(this) } @Test fun shouldPassCategoriesToView2() { `doAnswer`({ it: InvocationOnMock? -> (it!!.arguments[0] as CategoriesRepository.OnGetCategoriesListener).onSuccess(DUMMY_CATEGORIES) null }).`when`(repository).getCategoriesAsync(any(CategoriesRepository.OnGetCategoriesListener::class.java)) val presenter: CategoriesActivityPresenter = CategoriesActivityPresenter(repository) presenter.bindView(view) presenter.loadCategories() `verify`(view).displayCategories(DUMMY_CATEGORIES) } 这是测试的代码: override fun loadCategories() { repository.getCategoriesAsync(object: CategoriesRepository.OnGetCategoriesListener { override fun onSuccess(categories: […]

如何使用Mockito模拟kotlin ByteArray?

当我运行这个: class SomeTest { fun howToMockByteArray() { val bytes = Mockito.mock(ByteArray::class.java) } } 投掷: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class [B Mockito cannot mock/spy because : – VM does not not support modification of given type 我如何使用Mockito来模拟kotlin的ByteArray?

在junit测试中,反应器switchifempty的行为不像预期的那样

我正在为下面提供的方法编写测试。 ` class ScrapedRecipeCache @Autowired constructor(private val cache: RecipeScrapingCacheService, private val recipeService: RecipeService) : ScrapedRecipeProvider { override fun provide(request: ScrapingRequest): Flux<ScrapedRecipe> = cache.retrieve(request.link) .doOnNext { println(it) } .flatMap { (link, _, recipeHash, error) -> recipeService.findByHash(recipeHash) .map { ScrapedRecipe(it, link, error)} .switchIfEmpty(cache.remove(request.link).then(Mono.empty())) } .flux() } `测试看起来如下: private val recipeFetched = Recipe("Tortellini", RecipeDifficulty.EASY, 15.0) val cacheContents = […]

嘲笑嵌套的服务器响应

我有以下Json模型: /** * Json model of room response */ open class JsonRoom(val BookingData: JsonRoomBookingData, val GeneralData: JsonRoomGeneralData) /** * Json model of room booking data */ open class JsonRoomBookingData(val BookingId: Int, val BookingStatus: Int, val RoomId: Int, val RoomName: String, val TimeFrom: Date, val TimeTo: Date, val TimeFromUtc: Date, val TimeToUtc: Date, val Isrecurrence: Boolean, […]

是不是可以设定一个懒惰的财产预期?

以下测试产生一个NullPointerException 。 是不是可以设定一个懒惰的财产预期? class GarbTest { @Test fun xx(){ val aa = Mockito.mock(AA::class.java) Mockito.`when`(aa.bb).thenReturn("zz") } open class AA(){ val bb by lazy { "cc" } } }

Mockito从来没有()不工作,然后rxjava2

我正在测试用Kotlin编写的一段相当简单的代码: o1.updateUser(params) .andThen(o2.reload()) 当updateUser失败时,我预计重装将不会被执行。 但是当使用Mockito 2和jUnit 5进行测试时,就会调用这个模拟。 嘲笑是这样做的: given(o1.updateUser(user)).willReturn(Completable.error(IllegalArgumentException()) given(o2.reload()).willReturn(Completable.complete()) 现在的问题是,代码的行为如预期的,这意味着在这种情况下,o2是从来没有实际调用,但单元测试Mockito声称有一个o1的调用。 测试断言如下所示: verify(o2, never()).reload() 我在这里做错了吗? 有没有解决的办法? 我是不是很了解操作员?

如何模拟返回Observable的被动库

所以我有知识库,它提供了客户端的Observable。 有没有办法我可以嘲笑这个存储库,所以我不需要从我的模拟器发送位置或使用真实的设备获得一些位置? 接口如下所示: interface RxLocationRepository { @SuppressLint("MissingPermission") fun onLocationUpdate(): Observable<Location> fun stopLocationUpdates() } 在我的客户端,我使用这样的: class LocationManager( val rxLocationRepository: RxLocationRepository){ private fun appendGeoEvent(location: Location) { val locationGeoEvent = LocationGeoEvent( accuracy = location.accuracy.toDouble(), latitude = location.latitude, longitude = location.longitude, timestampGeoEvent = location.time ) processGeoEvent(locationGeoEvent) } compositeDisposable.add(rxLocationRepository.onLocationUpdate() .subscribe(Consumer { location -> appendGeoEvent(location) })) …. 所以我发送这个获得的位置我的appendGeoEvent方法。 我可以使用例如Mockito,但我不知道如何嘲笑这个存储库,所以我可以使用假的位置。 另外,我想用Kotlin。