Issue
As described in Mockito 2 docs we can now Mock final classes, so I tried to do it in a simple project, but the results are not proper.
Here is my project structure
Here is the org.mockito.plugins.MockMaker text file
mock-maker-inline
Here is a demo activity for testing
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
}
private fun onLoginPressed(email:String, password:String){
}
fun isEmailValid(email: String):Boolean{
return true
}
}
here is the test class
class LoginActivityTest{
@Test
fun checkLoginValidity(){
val lognActivity = Mockito.mock(LoginActivity::class.java)
assert(lognActivity.isEmailValid("yuo"))
}
}
The test should have passed, since I am always returning true, but here is what I get.
I have also marked and unmarked the resources directory as test resources root, but nothing really happened.
Can you point me out, where am I wrong?
Solution
You mocked your Activity
class, so all it's methods allways returns "default" value (for boolean
it is false
). You need to say Mockito
to call real method of class.
`when`(lognActivity.isEmailValid(ArgumentMatchers.any() ?: ""))
.thenCallRealMethod()
By the way, it is better to use that solution for abstract classes only, because you can't create instance of it with another way. If your class is not abstract, just create it (for Activity
test better to use specific tools).
Answered By - Ircover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.