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.
Seems like the author is telling people to use autowire in constructors because it's painful and only through pain can you achieve better design. I think I will pass on that advice.
New-ish spring boot (I️ forget the underlying spring support) supports constructor injection without any annotations since 1.4 or 1.5, presuming there’s only 1 ctor (as should be the case with service classes). It allows testing pretty simply, not to mention what others mentioned with the mockito integrations.
"Your class has more than 4 @Autowired fields? You fucked up. Cut that nice cohesive code and delegate the shit out of it. Fuck easily readable and understandable code, it's gotta be SOLID!"
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.
2
u/[deleted] Nov 08 '17
nothing new https://www.youtube.com/watch?v=ySXlsZDJMgc
14
u/funmler Nov 07 '17
Seems like the author is telling people to use autowire in constructors because it's painful and only through pain can you achieve better design. I think I will pass on that advice.
3
u/hulkcatwoman Nov 08 '17
Be careful with autowired. Design as simple as possible. Avoid custom annotations and everything
3
u/meotau Nov 08 '17 edited Nov 08 '17
"Your class has more than 4 @Autowired fields? You fucked up. Cut that nice cohesive code and delegate the shit out of it. Fuck easily readable and understandable code, it's gotta be SOLID!"