我是Junit测试的新手。我正在使用maven surefire插件,struts2-junit-plugin& junit4.12 用于执行struts2操作类的单元测试用例。以下是POM Entry。
<!-- 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>
我的Junit Test课程在
之下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);
}
}
但是在执行测试时,我收到如下错误。
TestAccountActionUsingStrutsTestCase&GT; StrutsJUnit4TestCase.setUp:210-&GT; StrutsJUn it4TestCase.initServletMockObjects:196»NoSuchMethod
click here for command prompt screenshot
以下是错误消息。
----------------------------------------------- --------测试 -------------------------------------------------- -----运行au.com.replenishment.galaxy.testcase.TestLogic测试运行: 1,失败:0,错误:0,跳过:0,经过的时间:0.015秒 - 在a u.com.replenishment.galaxy.testcase.TestLogic正在运行 au.com.replenishment.galaxy.testcase.TestAccountActionUsingSt rutsTestCase测试运行:2,失败:0,错误:2,跳过:0,时间 经过时间:0.44秒&lt;&lt;&lt; FAI LURE! - 在 au.com.replenishment.galaxy.testcase.TestAccountActionUsin gStrutsTestCase testUserNameErrorMessage(au.com.replenishment.galaxy.testcase.TestAcc ountActionUsingStrutsTestCase)经过的时间:0.44秒&lt;&lt;&lt;错误! java.lang.NoSuchMethodError: org.springframework.mock.web.MockServletContext(Lorg / springframework的/核心/ IO /的ResourceLoader):V
testUserNameCorrect(au.com.replenishment.galaxy.testcase.TestAccountA ctionUsingStrutsTestCase)经过的时间:0.44秒&lt;&lt;&lt;错误! java.lang.NoSuchMethodError: org.springframework.mock.web.MockServletContext(Lorg / springframework的/核心/ IO /的ResourceLoader):V
TestAccountActionUsingStrutsTestCase&GT; StrutsJUnit4TestCase.setUp:210-&GT; StrutsJUn it4TestCase.initServletMockObjects:196»NoSuchMethod
TestAccountActionUsingStrutsTestCase&GT; StrutsJUnit4TestCase.setUp:210-&GT; StrutsJUn it4TestCase.initServletMockObjects:196»NoSuchMethod测试运行:3,失败:0,错误:2,跳过:0
请告知如何解决此问题?
答案 0 :(得分:1)
NoSuchMethodError
表示您的不兼容的依赖版本。尝试通常struts2-junit-plugin
从同一struts2-core
添加groupId
和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>
答案 1 :(得分:1)
您缺少依赖项
运行测试时,模拟对象需要在类路径上。您应该将这些依赖项添加到已部署库中的正确版本控制问题中。