title: How to mock instance variable of class?
body: <p>How do i mock the variables instantiated at class level..I want to mock GenUser,UserData. how do i do it...</p>
  
  <p>I have following class</p>
  
  <pre><code>public class Source {
  
  private  GenUser v1 = new GenUser();
  
  private  UserData v2 = new UserData();
  
  private  DataAccess v3 = new DataAccess();
  
  public String createUser(User u) {
      return v1.persistUser(u).toString();
      }
  }
  </code></pre>
  
  <p>how do i mocked my v1 is like this</p>
  
  <pre><code>GenUser gu=Mockito.mock(GenUser.class);
  PowerMockito.whenNew(GenUser.class).withNoArguments().thenReturn(gu);
  </code></pre>
  
  <p>what i have written for unit test and to mock is that</p>
  
  <pre><code>@Test
  public void testCreateUser() {
      Source scr = new Source();
      //here i have mocked persistUser method
      PowerMockito.when(v1.persistUser(Matchers.any(User.class))).thenReturn("value");
      final String s = scr.createUser(new User());
      Assert.assertEquals("value", s);
  }
  </code></pre>
  
  <p>even if i have mocked persistUser method of GenUser v1 then also it did not return me "Value" as my return val.</p>
  
  <p>thanks in adavanced.......:D</p>