JavaWorld
addict
Reged: 06/20/03
Posts: 482
|
|
Unit test Struts applications
|
KSader
Unregistered
|
|
I'm surprised you didn't mention JMock for testing struts actions. I've personally found it easier to use than AOP.
|
Ramnivas Laddad
Unregistered
|
|
The problem dealt in this article is not EasyMock vs. JMock vs. AOP. Rather, it is how to inject your mock objects appropriately.
The core idea in the article is leveraging AOP with *a* mock framework to simplify testing; whether you use EasyMock or JMock or hand-written mock is fundamentally inconsequential here.
Good article, BTW.
-Ramnivas
|
Andres Almiray
Unregistered
|
|
I agree with Ramnivas that the goal of the article is to show a new way for testing Struts applications.
Dwelving deeper into the implementation details, StrutsTestCase and Spring enabled test cases will help you get a reference to the Action for mock injection.
|
Walter Jia
Unregistered
|
|
Thanks to Ramnivas for pointing out the core idea of the article.
The presented AOP solution applies to the integration of JMock and StrutsTestCase as well. Unlike EasyMock, JMock requires test cases to derive from its base class. This brings up the challenge of simulating multiple inheritance in Java, since StrutsTestCase is also inheritance based. The simulation is achievable by combining inheritance and composition. To keep the article focused on the role of AOP, I chose EasyMock over JMock for the discussion.
Walter
|
Unregistered
|
|
Hi,
I am posting the test class and the error I got while unit testing the struts application with easy mock.
Please, let me know if any one has a solution for the following error:
java.lang.IllegalStateException: incompatible return value type.
at controller.setReturnValue("Great");
code : ------
package com.unittest;
import java.util.ArrayList; import java.util.List;
import junit.framework.TestCase; import org.easymock.MockControl; import com.jamesholmes.minihr.*; public class EmployeeSearchServiceTest extends TestCase {
protected void setUp() throws Exception { super.setUp(); }
protected void tearDown() throws Exception { super.tearDown(); }
//Test searchByName() method in EmployeeSearchService class public void testSearchByName() { //define the mock object MockControl controller = MockControl. createControl(EmployeeSearchServiceIntf.class); final EmployeeSearchServiceIntf inf = (EmployeeSearchServiceIntf) controller.getMock(); // ArrayList myArr = new ArrayList(); //define the behavior of mock object inf.searchByName("Mary Williams"); controller.setReturnValue("Great"); controller.replay(); //use override technology to bridge from //mock object to the class to be tested EmployeeSearchService instance = new EmployeeSearchService() { protected EmployeeSearchServiceIntf getEmployeeSearchServiceIntf() { return inf; } }; //start test ArrayList result = instance.searchByName("Mary Williams"); //do verification between expected result //and actual result assertEquals("Woo!Great", result); //do verification on the mock object controller.verify(); } }
Error: -------
java.lang.IllegalStateException: incompatible return value type at org.easymock.internal.MocksControl.andReturn(MocksControl.java:90) at org.easymock.MockControl.setReturnValue(MockControl.java:162) at com.bofa.unittest.EmployeeSearchServiceTest.testSearchByName(EmployeeSearchServiceTest.java:81) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:164) at junit.framework.TestCase.runBare(TestCase.java:130) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:228) at junit.framework.TestSuite.run(TestSuite.java:223) at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
|