I have a class and I am injecting a proxy into my service.
Service service
{
private ServiceProxy proxy;
public Service(ServiceProxy proxy)
{
this.proxy = proxy;
}
}
The test for it is:
ServiceTest
{
@Mock
ServiceProxy mockProxy;
Service service = new Service(mockProxy);
}
If I initialize my class like this I always get a NPE
when I want to use the service object. Why does Mockito
do this? What is an easy way around this instead of declaring it in each and every test?
Provided you are using Mockito version 1.9.0 or later, the best way to achieve what you want is like this:
@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
@Mock
private ServiceProxy proxy;
@InjectMocks
private Service service;
@Test
public void test() {
assertNotNull(service);
assertNotNull(proxy);
}
}
First thing is the @RunWith(MockitoJUnitRunner.class)
declaration which will cause @Mock and @InjectMocks annotation to work automatically without any explicit initialization. The second thing is that starting with Mockito 1.9.0 @InjectMocks annotation can use the Constructor injection mechanism which is the best option for your Service class.
Other options for @InjectMocks are Setter injection and Field injection (see docs BTW) but you'd need a no argument constructor to use them.
So summarizing - your code cannot work because:
If for some reason you don't want to use @InjectMocks, the only way is to construct your Service object within the test method body or within the @Before annotated setUp method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With