【问题标题】:Struts2 Junit case throwing error during mvn testmvn测试期间的Struts2 Junit案例抛出错误
【发布时间】:2017-02-01 05:47:56
【问题描述】:

我是 Junit 测试的新手。我正在使用 maven surefire plugin, 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

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

【问题讨论】:

  • 请发布文字,而不是图片。
  • @DaveNewton :感谢您的建议。我已经更新了问题,使错误消息更加清晰,并将内联图像替换为图像链接。 :)

标签: java spring maven junit struts2


【解决方案1】:

你缺少依赖项

  • 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

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

【讨论】:

  • 感谢您的回复。正如您所建议的,我已在项目中添加了具有适当版本的上述依赖项。但我仍然遇到同样的错误。我不知道如何在类路径中添加模拟对象。你的意思是,我必须在构建路径中添加类文件夹吗?我做到了,但问题仍然没有解决。请查看 Yasser Zamani 发布的答案中的对话,我添加了有关该项目的更多详细信息。
  • 你能检查一下这个链接吗,我可以解决 Eclipse 中的问题。但不确定如何在 Maven 构建测试用例执行期间解决该问题。 stackoverflow.com/questions/42014478/…
【解决方案2】:

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>

【讨论】:

  • 感谢 Yasser 的回复,
  • 这个项目是从Ant build转成maven build的,所以我已经手动将所有的jar文件导入到了带有自定义组id的仓库中.正如您所建议的,我已将“junit 插件和 struts2-core”都映射到相同的组 ID“org.apache.struts”。但我仍然遇到同样的错误。还有什么我应该尝试的吗?
  • @aravind,是的,尝试从上到下添加所需的 maven 依赖项。例如我用struts2-spring-plugin 更新了我的答案,它也自动添加了spring-test。一般来说,尝试将你之前的ant项目依赖从上到下映射到maven依赖。
  • 我尝试使用 maven 依赖项而不是 ant,但是在应用程序中,大多数 Jar 没有版本号,并且一些 Jar 文件不存在于 maven 存储库本身中。如果我使用随机版本号,maven build / jboss 部署会因不同的错误而失败。
  • @aravind,jar 文件是 zip 文件,您可以使用 zip 软件打开并查看其 maven 文件夹以获取版本。如果丢失,请在网络中搜索{your jar name} maven 并选择它的发布日期与您的 jar 文件相同或接近。然后查看您的 maven 依赖层次结构中是否存在任何不匹配的依赖项。
猜你喜欢
  • 2013-11-01
  • 2013-05-25
  • 2018-06-27
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多