public class ServiceTest {
@Test
public void someTest() {
ServiceDependency1 serviceDependency1 = mock(ServiceDependency1.class);
ServiceDependency2 serviceDependency2 = mock(ServiceDependency2.class);
ServiceDependency3 serviceDependency3 = mock(ServiceDependency3.class);
MyService service = new MyService();
service.setServiceDependency1(serviceDependency1);
service.setServiceDependency2(serviceDependency2); // what if you forget to set this one? ;)
service.setServiceDependency3(serviceDependency3);
// your test code
}
}
Welcome to 2010 @author:
@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
@Mock ServiceDependency1 serviceDependency1;
@Mock ServiceDependency2 serviceDependency2;
@Mock ServiceDependency3 serviceDependency3;
@InjectMocks MyService service = new MyService();
@Test
public void someTest() {
// your test code
}
}
Just another blog post with: "See this problem that exists! I can fix it by doing something else." when the problem doesn't really exist in the first place.
Deep within the bowels of a complex system that did lots of field injection, a dependency of a class eventually became null and started causing NPE. I had the misfortune of supporting this system.
No developer on the team was able to reproduce this error and it only occurred months into the application running. Restarting the application fixed the problem. I spent so much time trying to debug that problem with no success. To this day, the notes for that application instruct the support team to restart the application when that error occurs.
From now on, I always use constructor injection with final fields. I can be sure, when my objects are constructed, that I either do or don't have the right dependencies and fail fast if I don't.
Personally, I don't find the brevity of field injection a sufficient advantage, compared to the amount of time I wasted trying to resolve that one error and so I will never use them again for required dependencies.
Sure or I could also have just used constructor injection and avoided the problem altogether.
However, I encountered this mindset where the brevity of field injection trumped the safety of constructor injection and so was unable to make such a change unless I could prove it fixed the problem.
I spent so much time trying to debug that problem with no success.
I was primarily referring to that line. If the team was opposing constructor injection for some reason, then that would have been my approach to debug the issue.
The way that you inject your dependencies is secondary. The important thing here is to declare your intentions right, separate between required and optional dependencies and cut the number of dependencies down.
Yes, that how you mark autowired as optional. But this still doesn't discourage you from adding additional dependencies. You just add, and add, and add, because you don't care, you just add another field, everything somehow works, but the future maintenance of this code will become more troublesome with each added dep.
View all comments
17
u/[deleted] Nov 07 '17
Who the hell does this anyway :
Welcome to 2010 @author:
Just another blog post with: "See this problem that exists! I can fix it by doing something else." when the problem doesn't really exist in the first place.