PowerMockito for Mocking and testing
Basics of simple Mocking a test class is given here:
http://thebadengineer.com/using-mockito-for-testing/
There is another way of mocking and unit testing i.e PowerMockito.So here is a small snippets:
class B{ protected void method1(){ //some code } protected void method2(args obj1 , string obj2){ //some code } protected Map<String,Object> method3() { Map<String,int> mp = new Hashmap<>(); mp.put("test1",1); return mp; } } B b; @Before public void setup() throws Exception { MockitoAnnotations.initMocks(this); //this is necessary for enabling the annotation b = PowerMockito.spy(new B()); //creating the object of another class uisng spy PowerMockito.doReturn(mockObjects).when(b, "method1"); //mocking the return value of another class method PowerMockito.doNothing().when(b, "method2",mockDataFetchingEnv, "simpleFilter"); //Mocking and doing nothing when method of different class is called with args int count = PowerMockito.doReturn(1).when(b, "method3"); assertTrue(count,1); }
We can use PowerMockito and Mockito together but not in the same class.I use PowerMockito to mock the method of different class and gets it return value.