Struts2 mvn 测试期间 Junit 案例抛出错误

Struts2 Junit case throwing error during mvn test

我是 Junit 测试新手。我正在使用 Maven surefire 插件,struts2-junit-plugin 和 junit4.12 来执行 struts2 操作 classes 的单元测试用例。下面是 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 测试 class

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

请指教如何解决这个问题?

NoSuchMethodError 表示您的依赖版本不兼容。尝试从相同的 groupId 添加 struts2-junit-pluginstruts2-core 依赖项,通常 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>

您缺少依赖项

  • 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

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