gpt4 book ai didi

java - Struts2 Junit case 在 mvn 测试期间抛出错误

转载 作者:行者123 更新时间:2023-11-30 02:40:14 26 4
gpt4 key购买 nike

我是 Junit 测试的新手。我正在使用 maven surefire 插件、struts2-junit-plugin 和 junit4.12 来执行 struts2 操作类的单元测试用例。下面是 POM 条目。

 <!-- 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 测试类如下

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);

}
}

但是在执行测试时,我收到错误:

下面是错误消息。

-------------------------------------------------------  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

请问如何解决这个问题?

最佳答案

您缺少依赖项

  • junit » junit 4.12
  • org.apache.struts » struts2-spring-plugin(可选)2.5.8
  • org.springframework » spring-test 4.1.6.RELEASE 4.3.6.RELEASE
  • org.springframework » spring-core 4.1.6.RELEASE 4.3.6.RELEASE
  • org.springframework » spring-context 4.1.6.RELEASE 4.3.6.RELEASE

运行测试时模拟对象需要位于类路径上。您应该添加这些依赖项来纠正已部署的库中的版本控制问题。

关于java - Struts2 Junit case 在 mvn 测试期间抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com