Android 编写用于测试rxjava的单元测试,但不确定我的单元测试是否正在测试所有内容
我正在测试以下方法。基本上,此测试所做的是发出一个请求,该请求将返回一个映射的Android 编写用于测试rxjava的单元测试,但不确定我的单元测试是否正在测试所有内容,android,kotlin,rx-java2,Android,Kotlin,Rx Java2,我正在测试以下方法。基本上,此测试所做的是发出一个请求,该请求将返回一个映射的LoginResponseEntity,并返回一个Single 我还能做些什么来测试这个方法吗。我应该测试.map{loginResponseDomainMapper.map(it)这个方法的一部分吗?这是一个非常小的方法,不包含太多要测试的内容。两个外部依赖项(loginAPIService和loginResponseDomainMapper)减少了更多要测试的内容 所以 1) loginResponseDomain
LoginResponseEntity
,并返回一个Single
我还能做些什么来测试这个方法吗。我应该测试
.map{loginResponseDomainMapper.map(it)
这个方法的一部分吗?这是一个非常小的方法,不包含太多要测试的内容。两个外部依赖项(loginAPIService
和loginResponseDomainMapper
)减少了更多要测试的内容
所以
1) loginResponseDomainMapper
不是测试方法的一部分,也应该被模拟
2) 你必须明白,这里应该测试什么
- 首先:检查
是否正确构造并传递给LoginRequestEntity
方法。这是通过loginUserPost
调用完成的。此外,您还可以使用verify(loginAPIService).loginUserPost(LoginRequestEntity,“apitonk”)
- 第二:将
方法的输出正确地传递给loginUserPost
方法。这可以通过前面的额外loginResponseDomainMapper.map
调用来完成verify
- 第三:map方法的输出已正确返回。这是通过调用
完成的assertValue
loginUserPost
没有@NotNull
注释,您最好处理此函数的null
结果。另外,如果请求不正确怎么办?密码错误,或者apitoken已过期?我相信这不会导致令人悲伤的后果,但您应该考虑一下。您可以为不成功的场景添加测试: 1) 如果
loginUserPost
失败,您可以添加:
@Test
fun `should return LoginResponse`() {
val loginRequestEntity = LoginRequestEntity("username", "password", "uniqueidentifier", "devicetoken")
val loginResponse = LoginResponse("token", createUser(), emptyList(), emptyList())
val loginResponseEntity = LoginResponseEntity("token", createUserEntity(), emptyList(), emptyList())
whenever(loginAPIService.loginUserPost(loginRequestEntity, "apitoken")).thenReturn(Single.just(loginResponseEntity))
loginServiceImp.loginUserPost("username", "password", "uniqueidentifier", "devicetoken", "apitoken")
.test()
.assertValue(loginResponse)
verify(loginAPIService).loginUserPost(loginRequestEntity, "apitoken")
}
private fun createUser() =
User(
"id",
"email",
"firstname",
"lastname",
"phone",
"address",
"dob",
"customer",
listOf("enterpriseids"),
listOf("vendorids"))
private fun createUserEntity() =
UserEntity(
"id",
"email",
"firstname",
"lastname",
"phone",
"address",
"dob",
"customer",
listOf("enterpriseids"),
listOf("vendorids"))
}
whenever(loginAPIService.loginUserPost(any(), any())).thenReturn(Single.error(Exception()))
...
.test()
.assertError(...)
2) 如果loginResponseDomainUpper
失败,您可以添加:
@Test
fun `should return LoginResponse`() {
val loginRequestEntity = LoginRequestEntity("username", "password", "uniqueidentifier", "devicetoken")
val loginResponse = LoginResponse("token", createUser(), emptyList(), emptyList())
val loginResponseEntity = LoginResponseEntity("token", createUserEntity(), emptyList(), emptyList())
whenever(loginAPIService.loginUserPost(loginRequestEntity, "apitoken")).thenReturn(Single.just(loginResponseEntity))
loginServiceImp.loginUserPost("username", "password", "uniqueidentifier", "devicetoken", "apitoken")
.test()
.assertValue(loginResponse)
verify(loginAPIService).loginUserPost(loginRequestEntity, "apitoken")
}
private fun createUser() =
User(
"id",
"email",
"firstname",
"lastname",
"phone",
"address",
"dob",
"customer",
listOf("enterpriseids"),
listOf("vendorids"))
private fun createUserEntity() =
UserEntity(
"id",
"email",
"firstname",
"lastname",
"phone",
"address",
"dob",
"customer",
listOf("enterpriseids"),
listOf("vendorids"))
}
whenever(loginAPIService.loginUserPost(any(), any())).thenReturn(Single.error(Exception()))
...
.test()
.assertError(...)
通过这种方式,您可以测试不成功/成功的映射和函数输出,而无需详细说明。
loginResponseDomainMapper
行为应该在其自己的单元测试范围内进行测试。通常,您测试的第一件事是函数的结果。您可以使用TestSchedulerUnless进行测试,而不需要使用某种代码覆盖e工具,您永远不会知道您的代码是否经过了全面测试,您可以尝试jacoco
,甚至idea
中包含一个。您只是在测试愉快的路径。如果loginapiersevice.loginUserPost()方法失败怎么办?
whenever(loginResponseDomainMapper.map(any())).thenThrow(Exception())
...
.test()
.assertError(...)