dimanche 25 juin 2017

How to call mock method instead of real method in mockito/Junit

I want to call DBConnection.mockExecuteUpdate() whenever veps.executeUpdate1() method is invoked.

public class DBConnection {
     public static int mockExecuteUpdate(String url, MySql sql){
        if(sql==null){
            return 0;
        }
        MySql.putTable(sql);
        return 1;
    }

}

Definition of executeUpdate1()
class Veps{
    protected synchronized int executeUpdate1(String url, MySql sql){
    ----
    -----
    }
}

public class VepsTest {
        public void setUp() throws Exception {

            veps = mock(Veps.class);
            when(veps.executeQuery1(any(String.class), any(MySql.class))).thenReturn(DBConnection.mockExecuteQuery(any(String.class),any(MySql.class)));
            when(veps.executeUpdate1(any(String.class), any())).thenReturn(DBConnection.mockExecuteUpdate(any(String.class),any()));
         }
}

But I get the exception for the second "when" callback

when(veps.executeUpdate1(any(String.class), any())).thenReturn(DBConnection.mockExecuteUpdate(any(String.class),any()));

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 4 recorded.
This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

Aucun commentaire:

Enregistrer un commentaire