如何在 Kotlin 中使用 JUnit Mockito 测试/模拟 API



我正在尝试使用 kotlin 在我的 Android 应用程序中编写一个单元测试。getPosts()https://jsonplaceholder.typicode.com/posts获取帖子列表

我想测试此功能,比较计数或您建议的任何其他方式。

我对此很陌生,所以任何更正建议都会非常有帮助

这是我到目前为止尝试过的。 您的帮助将不胜感激。

R

@Singleton
class PostsRemoteDataSource @Inject constructor(
private val postsService: PostsService
): PostsDataSource {
override fun getAllPosts(): Flowable<List<PostEntity>> {
return postsService.getPosts()
.flatMap { posts ->
Flowable.fromIterable(posts)
.take(10)
.toList().toFlowable()
}
}
}

邮政服务

interface PostsService {
@GET("/posts")
fun getPosts(): Flowable<List<PostEntity>>
}

测试用例

class PostsRemoteDataSourceTest {

@Before
fun init() {
MockitoAnnotations.initMocks(this)
}
@Test
fun getAllPosts() {
val postService = mock(PostsService::class.java)
val postsRemoteDataSource = PostsRemoteDataSource(postService)
var mList = listOf<PostEntity>()
Mockito.`when`(postService.getPosts()).thenReturn(Flowable.fromArray(mList))
val allPosts = postsRemoteDataSource.getAllPosts()
//PRETTY SURE THE LINE BELLOW IS WRONG I AM TRYING TO COMPARE THE 
//COUNT BUT CANNOT GET appPosts.size as it is Flowable type
assertEquals(allPosts , CoreMatchers.`is`(10))
}
}

更新

@Test
fun getAllPosts() {
val postService = mock(PostsService::class.java)
val postsRemoteDataSource = PostsRemoteDataSource(postService)
var mList = listOf<PostEntity>(
PostEntity(1,1,"Title 1", "Body 1"),
PostEntity(1,1,"Title 1", "Body 1")
)
Mockito.`when`(postService.getPosts()).thenReturn(Flowable.fromArray(mList))
val subsciber = postsRemoteDataSource.getAllPosts().test()

assertEquals(subsciber.assertValueCount(10) , 10)
}
}

运行测试时出现错误消息


at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:189)
at io.reactivex.observers.BaseTestConsumer.assertValueCount(BaseTestConsumer.java:515)
at com.rao.com.idealarchitecture.data.remote.PostsRemoteDataSourceTest.getAllPosts(PostsRemoteDataSourceTest.kt:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

选项 1

使用blockingFirst()通过阻止调用获取数据:

val allPosts = postsRemoteDataSource.getAllPosts().blockingFirst()
assertEquals(10, allPosts.size)

请注意,在生产代码中,对Flowable进行阻塞调用通常被认为是不好的做法,但在测试中这样做是安全的,因为受测对象由模拟支持。

选项 2

通过调用test()并对其断言来获取TestSubscriber

val subscriber = postsRemoteDataSource.getAllPosts().test()
subscriber.assertValueCount(10)

相关内容

  • 没有找到相关文章

最新更新