Issue
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 ?
Solution
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.
Answered By - Roman C
Answer Checked By - Gilberto Lyons (JavaFixing Admin)