I want to mock a concrete class in a TestNG test case. The class could look like this (simplified example):
public class Example() {
  private MyHello myHello;
  public Example(MyHello myHello) {
    this.myHello = myHello;
  }
  public String doSomething() {
    return myHello.doSomethingElse();
  }
}
Now we want to mock Example return some defined value:
@BeforeMethod
public void setUp() {
  this.example = mock(Example.class);
  when(this.example.doSomething()).thenReturn("dummyValue");
}
This looks quite good but in fact it isn't. The last line in the setup method calls the method on an instance of Example, this instance didn't get an MyHello through the constructor and so I get a NPE in the setUp method.
Is there a way to either inject an MyHello while creating the mock or to disallow Mockito calling the method on a real instance?
Edit
The issue, that caused the observed behaviour was, that the doSomething() method is actually final. I overlooked that when I tried to solve that issue. And this is a known limitation with mockito anyway. So I'll either remove the finals or extract an interface for that class.
 
     
     
    