title: Mockito: mocking class always calls real method
body: <p>I would like to mock a class.</p>
  
  <p>The class is called like this:</p>
  
  <p>here's my code:</p>
  
  <pre><code>@Mock
  SomeClass someClass;
  
  @InjectMocks
  ToBeTested toBeTested;
  
  @Before
  public void setUp() {
      MockitoAnnotations.initMocks(this);
  }
  
  // in the test:
  doReturn(returnValue).when(someClass).doSomething(param1, param2); 
  </code></pre>
  
  <p>I feel like I have tried every possible combination of <code>@Mock</code> and <code>@Spy</code> and <code>doReturn</code> and <code>when</code>, but instead of mocking the method call, the real method is called and an NPE is thrown.</p>
  
  <p>How do I do this correctly?</p>
  
  <p>I'll provide more code if needed.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>both <code>SomeClass</code> and <code>doSomething()</code> are public and neither are final.</p>
  
  <p>I tried using <code>MockitoJunitRunnerclass</code> instead of <code>MockitoAnnotations</code>and the exception is still thrown.</p>
  
  <p>The class to be tested:</p>
  
  <pre><code>@Component
  public class ToBeTested implements Something {
  
      @Override
      public ReturnValue doSomeAction(Parameter theParam) {
          try {
              SomeClass theClass = new SomeClass();
              MyReturnValue myReturnValue = theClass.doSomething(
                      parameterOfTypeInputStream,
                      parameterOfTypeString
              );
  
          // other stuff
  
              return theParam;
          } catch (IOException e) {
              throw new RuntimeException("Oh no!");
          }
      }
  
  // more
  </code></pre>
  
  <p><code>param1</code> and <code>param2</code> are of type InputStream and String respectively.</p>