Java mvn测试期间Struts2 Junit案例抛出错误
我不熟悉Junit测试。我使用mavensurefire插件、struts2 junit插件和junit4.12来执行struts2动作类的单元测试用例。下面是POM条目Java mvn测试期间Struts2 Junit案例抛出错误,java,spring,maven,junit,struts2,Java,Spring,Maven,Junit,Struts2,我不熟悉Junit测试。我使用mavensurefire插件、struts2 junit插件和junit4.12来执行struts2动作类的单元测试用例。下面是POM条目 <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit&l
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>au.com.mercury.lib</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4</version>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
朱尼特
朱尼特
4.12
测试
org.hamcrest
汉克雷斯特岩芯
1.3
org.apache.struts
struts2 junit插件
2.3.4
测试
au.com.mercury.lib
struts2型芯
2.3.4
org.apache.maven.plugins
maven surefire插件
2.19.1
org.apache.maven.surefire
surefire-junit47
2.19.1
方法
10
下面是我的Junit测试类
package au.com.replenishment.galaxy.testcase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.struts2.StrutsJUnit4TestCase;
import org.junit.Test;
import com.opensymphony.xwork2.ActionProxy;
import au.com.replenishment.galaxy.web.actions.implementation.AccountAction;
public class TestAccountActionUsingStrutsTestCase extends StrutsJUnit4TestCase<Object>{
@Test
public void testUserNameErrorMessage() throws Exception {
request.addParameter("accountBean.userName", "Bruc");
request.addParameter("accountBean.password", "test");
ActionProxy proxy = getActionProxy("/createaccount");
AccountAction accountAction = (AccountAction) proxy.getAction();
proxy.execute();
assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present", accountAction.getFieldErrors().size() == 1);
assertTrue("Problem field account.userName not present in fieldErrors but it should have been",
accountAction.getFieldErrors().containsKey("accountBean.userName") );
}
@Test
public void testUserNameCorrect() throws Exception {
request.addParameter("accountBean.userName", "Bruce");
request.addParameter("accountBean.password", "test");
ActionProxy proxy = getActionProxy("/createaccount");
AccountAction accountAction = (AccountAction) proxy.getAction();
String result = proxy.execute();
assertTrue("Problem There were errors present in fieldErrors but there should not have been any errors present", accountAction.getFieldErrors().size() == 0);
assertEquals("Result returned form executing the action was not success but it should have been.", "success", result);
}
}
package au.com.requirement.galaxy.testcase;
导入静态org.junit.Assert.assertEquals;
导入静态org.junit.Assert.assertTrue;
导入org.apache.struts2.StrutsJUnit4TestCase;
导入org.junit.Test;
导入com.opensymphony.xwork2.ActionProxy;
导入au.com.requirement.galaxy.web.actions.implementation.AccountAction;
公共类TestAccountActionUsingStrutsTestCase扩展了StrutsJUnit4TestCase{
@试验
public void testUserNameErrorMessage()引发异常{
addParameter(“accountBean.userName”、“Bruc”);
addParameter(“accountBean.password”、“test”);
ActionProxy=getActionProxy(“/CreateCount”);
AccountAction=(AccountAction)proxy.getAction();
proxy.execute();
assertTrue(“问题fieldErrors中不存在错误,但应该存在一个错误”,accountAction.getFieldErrors().size()==1);
assertTrue(“问题字段account.userName不存在于fieldErrors中,但应该存在”,
accountAction.getFieldErrors().containsKey(“accountBean.userName”);
}
@试验
public void testUserNameCorrect()引发异常{
addParameter(“accountBean.userName”、“Bruce”);
addParameter(“accountBean.password”、“test”);
ActionProxy=getActionProxy(“/CreateCount”);
AccountAction=(AccountAction)proxy.getAction();
字符串结果=proxy.execute();
assertTrue(“问题fieldErrors中存在错误,但不应该存在任何错误”,accountAction.getFieldErrors().size()==0);
assertEquals(“执行操作的表单返回的结果不是成功的,但应该是。”,“成功”,结果);
}
}
但是在执行测试时,我得到了一个错误:
下面是错误消息
------------------------------------------------------- T E S T S -----------
--------------------------------------------
Running au.com.replenishment.galaxy.testcase.TestLogic Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec - in au.com.replenishment.galaxy.testcase.TestLogic
Running
au.com.replenishment.galaxy.testcase.TestAccountActionUsingStrutsTestCase Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.44 sec
<<< FAILURE! - in au.com.replenishment.galaxy.testcase.TestAccountActionUsingStrutsTestCase testUserNameErrorMessage(au.com.replenishment.galaxy.testcase.TestAccountActionUsingStrutsTestCase) Time elapsed: 0.44 sec
<<< ERROR!
java.lang.NoSuchMethodError:
org.springframework.mock.web.MockServletConte.<init>(Lorg/springframework/core/io/ResourceLoader;)V
testUserNameCorrect(au.com.replenishment.galay.testcase.TestAccountActionUsingStrutsTestCase) Time elapsed: 0.44 sec
<<< ERROR!
java.lang.NoSuchMethodError:
org.springframework.mock.web.MockServletConte.<init>(Lorg/springframework/core/io/ResourceLoader;)V
Results :
Tests in error:
TestAccountActionUsingStrutsTestCase StrutsJUnit4TestCase.setUp:210-StrutsJUnit4TestCase.initServletMockObjects:196 » NoSuchMethod
Tests run: 3, Failures: 0, Errors: 2, Skipped: 0
---------------------------------------T-----------
--------------------------------------------
运行au.com.requipment.galaxy.testcase.TestLogic测试运行:1,故障:0,错误:0,跳过:0,运行时间:0.015秒-在au.com.requipment.galaxy.testcase.TestLogic中
跑步
au.com.requipment.galaxy.testcase.TestAccountActionUsingStrutsTestCase测试运行:2,失败:0,错误:2,跳过:0,所用时间:0.44秒
NoSuchMethodError
表示您的依赖项版本不兼容。尝试从相同的groupId
中添加struts2 junit插件
和struts2核心
依赖项,通常org.apache.struts
然后再试试
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4</version>
</dependency>
org.apache.struts
struts2弹簧插件
2.3.4
您缺少依赖项
- junit»junit 4.12
- org.apache.struts»struts2 spring插件(可选)2.5.8
- org.springframework»spring测试4.1.6.RELEASE 4.3.6.RELEASE
- org.springframework»spring核心4.1.6.RELEASE 4.3.6.RELEASE
- org.springframework»spring上下文4.1.6.RELEASE 4.3.6.RELEASE
运行测试时,模拟对象必须位于类路径上。您应该添加这些依赖项以纠正部署库中的版本控制问题。请发布文本,而不是图像。@DaveNewton:谢谢您的建议。我更新了问题,进一步澄清了错误信息,并将内联图像替换为图像链接。:)感谢Yasser的响应,这个项目已经从Ant build转换为maven build,所以我已经用一个自定义组id将所有jar文件手动导入到存储库中。正如您所建议的,我已经将“junit plugin&struts2 core”映射到同一个组id“org.apache.struts”。但我还是犯了同样的错误。还有什么我应该试试的吗?@aravind,是的,试着从上到下添加所需的maven依赖项。e、 g.我用struts2-spring插件更新了我的答案,该插件还自动添加了spring测试。通常,尝试从上到下将以前的ant项目依赖项映射到maven依赖项。我尝试使用maven依赖项而不是ant,但是在应用程序中,大多数Jar没有版本号,并且一些Jar文件不在maven存储库中。如果我使用随机版本号,maven build/jboss部署将失败,并出现不同的错误。@aravind,jar文件是zip文件,您可以使用zip软件打开它们的maven文件夹以获取版本。如果丢失,在网络中搜索{yourjarname}maven
,并选择它的发布日期与您的jar文件相同或相近。然后查看maven dependencies层次结构,查找任何不匹配的依赖项。感谢您的回复。正如您所建议的,我已经在项目中添加了具有适当版本的上述依赖项。但我还是犯了同样的错误。我不知道如何在t中添加模拟对象