How to use @InjectMocks and initMocks() with an object that has a required String parameter?
I think the simple answer is not to use @InjectMocks
, and instead to initialise your object directly. The only downside I can see is that you're not testing the injection, but then with @InjectMocks
, I think you'd be testing it with Mockito's injection implementation, rather than your real framework's implementation anyway, so no real difference.
I'd do:
public class MyServiceTests {
@Mock
private SomeObject someObject;
private MyService service;
@Before
public void setUp() {
service = new MyService("foo", someObject);
}
}