Unicorni
Unicorni

Reputation: 1

Test if a method was called without providing parameters

I have the following code:

class MyClass {

  def someMethods(): Unit = {
    val result1 = method1()
    val result2 = method2(result)     
  }
}

No I want to test if method1 and method2 are called, when I run someMethod.

class TestMyClass {

  @Test
  def testSomeMethods(): Unit = {
    val myClass = new MyClass()
    val myClassSpy = Mockito.spy(myClass)
    myClassSpy.someMethods()
    verify(myClassSpy).method1()
  }
}

For method1 this is working, but method2 needs a parameter, that is provided by method1.
Can I not just do something like assertTrue(method2.called)?
Because I do not want to check the result of the methods, I just want to know if they were called.

Upvotes: 0

Views: 62

Answers (1)

ultrasecr.eth
ultrasecr.eth

Reputation: 1497

Hmm, using a spy for that is already a smell, you shouldn't be testing internals of your class like that.

In any case you could do Mockito.verify(myClassSpy).method2(Mockito.any()) and that would do the trick, but I'd seriously review your design and the motivation behind this test as it really feels like the wrong approach.

Upvotes: 3

Related Questions