弹簧& Struts2 REST - junit 测试
我在 Spring 中注入了三个层次结构 - 休息、业务逻辑和数据库操作。 BL 和 DAO 的 Junit 测试工作正常,而其余部分只能注入业务逻辑 ioc 层。
我的 junit 测试的晚餐课程:
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
};
}
}
所以 - 其余调用业务逻辑,这调用数据库操作。空指针异常属于数据库调用的业务逻辑。
更多信息与示例: REST:getUser(id) 调用 BL: getUserBO(id) 调用 DAO: getUserDAO(id)
在 getUserBO 方法中的 getUserDAO 上抛出空指针。这种情况仅发生在正在部署的 junit 测试中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有定义数据源bean吗?没有更多细节(例如堆栈跟踪、应用程序上下文文件等),这是我最好的猜测......
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 ...
我发现是struts2的rest class映射的问题。所以 Spring 无法注入...
I found that it is a problem of struts2 rest class mapping. So Spring couldn't inject...