mercredi 13 février 2019

Kotlin - Mockito verify method calls

I'm trying my hand with Mockito for writing unit test's. I have a class that needs to be tested like below-

open class Employee {
  fun setDetails(name: String, age: Int) {
    setName(name)
    setAge(age)
  }

  fun setName(name: String) { }

  fun setAge(age: Int) { }
}

Below is my test class

class EmployeeTest {
  @Mock
  lateinit var emp: Employee

  @Before
  fun setup() {
    MockitoAnnotations.initMocks(this)
  }

  @Test
  fun testDetail() {
    emp.setDetails("Henry", 23)

    verify(emp, times(1)).setAge(23)
  }

}

Here is my problem

When I do -

verify(emp, times(1)).setAge(23)

This give's me a success, because setAge is called once in setDetails() of Employee.kt. So that works fine for me

But, when I do-

verify(emp, never()).setAge(23)

This still gives me a success, even though the method is called in setDetails(). Shouldn't this test case fail?

Please help me understand this. I haven't been able to figure out why this happens.

Aucun commentaire:

Enregistrer un commentaire