I have three hierarchical layers injected in Spring – rest, business logic and database operations. Junit tests for BL and DAO are working OK, when rest can inject only business logic ioc layer.
My supper class for junit tests:
import org.springframework.test.AbstractTransactionalSpringContextTests;
public class AbstractTest extends AbstractTransactionalSpringContextTests {
protected static final String path = "config/spring/applicationContext.xml";
/**
* Disabled autowire by type
* Disabled dependency check
*/
public AbstractTest() {
super();
this.setAutowireMode(AUTOWIRE_BY_NAME);
this.setDependencyCheck(false);
}
@Override
protected String[] getConfigLocations() {
return new String[] {
path
};
}
}
So – rest calls business logic and this calls database operations. Nullpointer exception falls in business logic for database calls.
More info with example:
REST: getUser(id) calls
BL: getUserBO(id) calls
DAO: getUserDAO(id)
Nullpointer is thrown on getUserDAO in getUserBO method. This only happens with junit tests it is working deployed.
I found that it is a problem of struts2 rest class mapping. So Spring couldn’t inject…