Auto wiring Spring Test Cases

Featured image

Spring is of late being used universally. Hence the need arises to either

This code gets repetitive across tests thereby making us wish if we could do something for re-use. Here is a simple SpringJUnitTestBase class which does exactly that. The code now becomes the following:

public class MyTestCase extends SpringJUnitTestBase {
// This will get auto-wired by name.
protected Service service;
public void setService(Service s){
service = s;
}

public void setUp(){
}
}

Far simpler huh?? The only caveat is that the spring file should have the exact same name and be in the same package as the test case. So in this case we need to have MyTestCase.xml in the same package.