views:

439

answers:

2

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.

A: 

do you have a data source bean defined? without more details (such as stack trace, application context files, etc.), that's the best guess i have ...

LES2
I have edited the question. The stack trace and appContext is (i guess) meaningless?
Trick
can you add some logging statements to the setter method for the userDao (or which ever object is null) to verify that it is initialized by spring during the tests?before calling the dao, assert that it is not null as follows:UserDao dao = getUserDao();assert( dao != null ) : "UserDao is null: BUG!!!";
LES2
A: 

I found that it is a problem of struts2 rest class mapping. So Spring couldn't inject...

Trick