我正在使用struts2-junit-plugin编写struts2 web应用程序的测试用例,我的问题是,在我的操作类中,有一些与数据库相关的查询,它们使用数据源(Jndi),如何在我的测试用例中模拟这一点。
编辑
在此测试中,我将设置远程用户。
public void testexecute()
{
try
{
ActionProxy proxy = getActionProxy("/index");
IndexAction action = (IndexAction) proxy.getAction();
request.setRemoteUser("Haider");
assertTrue(action.execute().equals(ActionSupport.SUCCESS));
assertTrue(true);
}
catch(Exception ex)
{
assertTrue(false);
}
}在IndexAction (实现PrincipalAware)中,我有以下内容
public String execute()
{
try
{
if(principleProxy != null)
{
userModel = new UserModel();
userModel.setUserName(principleProxy.getRemoteUser());
}
else
{
return ERROR;
}
................................
.................................
}在索引中,当我运行测试时,principleProxy是空的。
发布于 2014-08-01 17:45:00
您可以查看如何使用德布尼特
DbUnit是一个JUnit扩展(也可用于Ant),其目标是数据库驱动的项目,其中包括在测试运行期间将数据库置于已知状态。这是一种很好的方法,可以避免当一个测试用例损坏数据库并导致后续测试失败或加剧损坏时可能出现的无数问题。 DbUnit具有将数据库数据导出和导入到XML数据集和从XML数据集导入数据库数据的能力。从2.0版开始,DbUnit也可以在流模式中使用非常大的数据集。DbUnit还可以帮助您验证数据库数据是否与预期的值集匹配。
https://stackoverflow.com/questions/25084344
复制相似问题