1

I am new to Junit testing. I am using maven surefire plugin, struts2-junit-plugin & junit4.12 for to executing unit test cases for struts2 action classes. Below is the 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>

And my Junit Test class is below

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

But when executing the test, I am getting the error:

Below is the error message.

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

Please advice how to resolve this issue ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aravind
  • 1,145
  • 6
  • 18
  • 44

2 Answers2

1

NoSuchMethodError means your incompatible dependency versions. Try to add both struts2-junit-plugin and struts2-core dependencies from same groupId usually org.apache.struts.

Then also try

<!-- 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 Zamani
  • 2,380
  • 21
  • 18
  • Thanks for responding Yasser, – Aravind Feb 01 '17 at 22:28
  • This project is converted from **Ant build** to **maven build**, so I have manually imported all the jar files into the repository with a custom group id. As you advised, I have mapped both 'junit plugin & struts2-core' to the same group ID 'org.apache.struts'. But still I am getting the same error. Is there anything else I should try? – Aravind Feb 01 '17 at 22:38
  • @aravind, yes, try add needed maven dependencies from top to down. e.g. I updated my answer with `struts2-spring-plugin` which adds `spring-test` also automatically. In general, try to map your previous ant project dependencies to maven dependencies from top to down. – Yasser Zamani Feb 02 '17 at 07:06
  • I tried with maven dependencies instead of ant but, in the application, most of the Jars are not having the version number and some Jar files are not present in maven repository itself. If I use a random version number, the maven build / jboss deployment is getting failed with different errors. – Aravind Feb 03 '17 at 03:24
  • @aravind, jar files are zip files which you can open with zip software and see their maven folder to get version. If missing, search `{your jar name} maven` in net and select which it's release date is same or near to your jar file. Then see your maven dependencies hierarchy for any not well matched dependency. – Yasser Zamani Feb 03 '17 at 09:21
1

You are missing dependencies

  • junit » junit 4.12
  • org.apache.struts » struts2-spring-plugin (optional) 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

Mock objects required to be on classpath when running tests. You should add these dependencies to correct versioning problem in your deployed libraries.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for responding. As you advised I have added the mentioned dependencies with appropriate versions in the project. But Still I am getting the same error. I do not know how to add mock objects in the class path. Do you mean, I have to add the classes folder in the build path?, I did it, but still the issue is not resolved. Please look into the conversation in the answer posted by Yasser Zamani, I have added further details about the project. – Aravind Feb 03 '17 at 01:57
  • Can you please check this link, I am able to resolve an Issue in eclipse. but not sure how to resolve the issue during maven build test case execution. http://stackoverflow.com/questions/42014478/struts-2-junit-test-case-execution-failed-strutstestcase-getactionproxy138-n – Aravind Feb 03 '17 at 03:51