>我正在我的Android应用程序中编写一个简单的JUnit测试,我正在测试收到的记录数,但我收到错误java.lang.AssertionError: Value counts differ; Expected: 10, Actual: 1 (latch = 0, values = 1, errors = 0, completions = 1)
这是我正在尝试测试的内容,它是一个从 API 获取 POSTS 数据的函数。
我的实际也是 10,但我不确定我哪里做错了。
我对此很陌生,你能建议一下吗
测试
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 subsciber = postsRemoteDataSource.getAllPosts().test()
assertEquals(subsciber.assertValueCount(10) , 10)
}
}
后远程数据源
@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>>
}
这是错误消息
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:38)
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)```
assertEquals(subsciber.assertValueCount(10), 10)
我觉得问题出在这个代码上,assertValueCount 本身就是比较检查。 在此基础上添加 assertEquals,使其与 assertValueCount 函数的返回值进行比较。
因此,正确的方法是:
subsciber.assertValueCount(10)
请试试这种方式!