How to trick the application context

How do we mock the application context? I have a presenter for which I am going to write a test. The parameters obtained were vie

w and Context

. How do I create a layout for the context to work?

public TutorProfilePresenter(TutorProfileScreenView view, Context context){
     this.view = view;
     this.context = context
}

public void setPrice(float price,int selectedTopics){
      int topicsPrice = 0;
      if(selectedTopics>2)
      {
        topicsPrice = (int) ((price/5.0)*(selectedTopics-2));
      }


      view.setBasePrice(price,topicsPrice,selectedTopics,
                        price+topicsPrice);
}

      

+5


source to share


1 answer


I would use the Mockito annotations as a basis (I assume you want to model the view as well):

public class TutorProfilePresenter{

   @InjectMocks
   private TutorProfilePresenter presenter;

   @Mock
   private TutorProfileScreenView viewMock;
   @Mock
   private Context contextMock;

   @Before
   public void init(){
       MockitoAnnotations.initMocks(this);
   }

   @Test
   public void test() throws Exception{
      // configure mocks
      when(contextMock.someMethod()).thenReturn(someValue);

      // call method on presenter

      // verify
      verify(viewMock).setBasePrice(someNumber...)
   }

}

      



This will allow you to inject ready-made layouts into the class under test.

More on the Mockito stub: sourceartists.com/mockito-stubbing

+6


source







All Articles