22
interface LoginDisplay {
    var username: String
    var password: String
}


class LoginActivityLoginDisplay : LoginDisplay {

    override var username: String
        get() = usernameEditView.text.toString()
        set(value) {
            usernameEditView.setText(value)
        }

    override var password: String
        get() = passwordEditView.text.toString()
        set(value) {
            passwordEditView.setText(value)
        }

}

This is the example of code I'd like to test with Mockito as follows:

verify(contract.loginDisplay).username

Tricky thing is - that in this call i can only verify getter of field username, meanwhile I'd like to test call on setter of this field.

Any help?

przebar
  • 531
  • 1
  • 5
  • 19

1 Answers1

67

It's simpler than you think. Calling:

verify(contract.loginDisplay).username = ""

will have the result you want. Setter setUsername on the mock of contract.loginDisplay will be called.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
atok
  • 5,880
  • 3
  • 33
  • 62
  • Also be sure to check that the verified property is marked as **open**... took me a while before actually reading mockito message about that – Gabriel Rohden Nov 26 '20 at 00:01