簡體   English   中英

Struts2 Junit case 在 mvn 測試期間拋出錯誤

[英]Struts2 Junit case throwing error during mvn test

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

請建議如何解決這個問題?

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 » 彈簧測試 4.1.6.RELEASE 4.3.6.RELEASE
  • org.springframework » 彈簧核心 4.1.6.RELEASE 4.3.6.RELEASE
  • org.springframework » 彈簧上下文 4.1.6.RELEASE 4.3.6.RELEASE

運行測試時需要在類路徑上的模擬對象。 您應該添加這些依賴項以更正已部署庫中的版本控制問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM