title: How to mock creation new object inside some service?
body: <p>I have to test a method inside some class like this:</p>
  
  <pre><code>public class aClassToTest{
    private SomeService someService = new SomeService();
  
    public String methodToTest(){
    String data = someService.getData();
    //....
   }
  }
  </code></pre>
  
  <p>So, I have mocked SomeService class for returning my mock-object instead of original SomeService-object. I have done it via PowerMockito in every of my <code>@Test</code> method</p>
  
  <pre><code>SomeService someServiceMock = mock(SomeService.class); 
  when(someServiceMock.getData().thenReturn(Data myMockedData)
  PowerMockito.whenNew(SomeService.class).withAnyArguments().thenReturn(someServiceMock);
  </code></pre>
  
  <p>And I have this annotation above my Test class:</p>
  
  <pre><code>@PrepareForTest({aClassToTest.class, SomeService.class})
  </code></pre>
  
  <p>It works fine if there is only one test, but if there is a few tests  <code>someServiceMock.getData()</code> returns data from the very first test each time dispite the fact, that i mock it in every test with new data. I've tried to add annotation <code>@PrepareForTest({aClassToTest.class, SomeService.class})</code>  above every <code>@Test</code> method, but now I have an <code>OutOfMemoryError</code> after a few tests, and now it works only if I run whole Test class with all test methods, but if I run test methods separately I have <code>No tests found for given includes</code> error. </p>
  
  <p>I have test class like this:</p>
  
  <pre><code>@RunWith(PowerMock.class)
  @PrepareForTest({aClassToTest.class, SomeService.class})
  public class TestClass{
  
  private void doMockSomeService(String testData){
    SomeService someServiceMock = mock(SomeService.class); 
    when(someServiceMock.getData().thenReturn(testData);
    PowerMockito.whenNew(SomeService.class).withAnyArguments().thenReturn(someServiceMock);
   }
  
    @Test
    public void testCase1(){
    String expectedResult = "expectedResult1";
    doMockSomeService("123");
    ClassToTest classToTest = new ClassToTest();
    String result = classToTest.methodToTest();
    assertEquals(result, expectedResult);
    }
  
   @Test
    public void testCase2(){
    String expectedResult = "expectedResult2";
    doMockSomeService("456");
    ClassToTest classToTest = new ClassToTest();
    String result = classToTest.methodToTest();
    assertEquals(result, expectedResult);
    }
  }
  </code></pre>
  
  <p>In this case, the return value of someService.getData() is always "123".</p>