mercredi 23 mai 2018

How to mock classes with constructor injection

How to get constructor injection in Mockito

I have the following class:

class A{

  private B mB;

  A(B b){
     mB = b;
  }

 void String someMethod(){
     mB.execute();
  }
}

how do I test someMethod using a mock class A and class B using

B b = Mockito.mock(B.class)
Mockito.when(b.execute().thenReturn("String")

A a = Mockito.mock(A.class)
//somehow inject b into A and make the below statement run
Mockito.when(a.someMethod()).check(equals("String"))

Aucun commentaire:

Enregistrer un commentaire