DeaIss
DeaIss

Reputation: 2585

Junit and Mockito: How to test whether a method calls a method?

I'm trying to wrap my head around mockito and was wondering how I would test if a method calls a method!

So here is class with its method inside it:

public class RegisterController {

    public void regHandle(UserDataObject user1){

        ValidateRegisterInputController validate = new ValidateRegisterInputController();
        validate.validateInputHandle(user1); }

How would I test that regHandle(UserDataObject) calls validate.validateInputHandle(user1); ?

I'm sure this is a super simple test, but I really can't figure out how to test this.

Upvotes: 1

Views: 976

Answers (2)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

There are various ways of writing a test for a method which instantiates some other class. I wrote about two of them in my article on the Mockito wiki, at http://code.google.com/p/mockito/wiki/MockingObjectCreation

Both the techniques that I describe involve refactoring your code to make it more testable.

Upvotes: 3

Jose Sutilo
Jose Sutilo

Reputation: 918

You would create a mock of ValidateRegisterInputController and then pass it on construction, then you would do: Mockito.verify(mock).validateInputHandle(user1).

I strongly suggest you do not do this type of testing though. Instead of that, ask yourself how can you write an unit test that checks that what you wanted to validate was valid.

for example, check that after calling regHandle user1.isValid() is equals to true.

Upvotes: 1

Related Questions