My Service class has several autowired objects (Stored Procedures, SPs). Rather than a MockBean when/then, one test actually requires me to autowire the SP dependency objects in full, to see which object is being returned.
I can't really construct the dependencies and wire them myself like suggested in this thread (e.g. via ReflectionTestUtils.setField(service, "myService", new SP());). Unfortunately, for these Stored Proc objects, there's no easy constructor for you. They extend org.springframework.jdbc.object.StoredProcedure where the only possible constructor is with an internal datasource, public MySP(@Autowired DataSource dataSource) { .. }.
@Service
public class MyService {
@Autowired
StoredProc1 storedProc1;
@Autowired
StoredProc2 storedProc2;
@Autowired
StoredProc3 storedProc3;
// NEED TO UNIT-TEST THIS METHOD
public StoredProc getStoredProc(Params params) {
// ...
// returns one of the 3 autowired objects
}
public mainServiceMethod(Params params) {
// Interface implemented by all autowired StoredProc objects
StoredProc storedProc = getStoredProc(params);
// ...
storedProc.someAction();
}
}
In this example I need to jUnit getStoredProc which returns one of my normally-autowired objects, and see what's being returned (it's like a factory method). But all these objects are NULL during the course of testing, and I can't construct them myself due to the lack of a user-invokable constructor. So I need the full autowired hierarchy to be available for this test. How do I achieve this?
jUnit:
@SpringBootTest
public class MyServiceTest {
@Autowired
MyService myService; // actual service is autowired
// Tried also autowiring the SP Dependencies, but they are NULL
}
I found the solution. I can
@Autowirethe Dependency Objects, which will construct them through the Spring configuration with no problem, and then the Service object can be@InjectMockson which I will set those fields manually withReflectionUtils.setField(..).Code:
Voila!