Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Expextations return an Object that then mocks a method

quinn

2009-08-15

Replies: Find Java Web Hosting

Author LoginPost Reply

I am new to JMock and trying to apply it to some JMS code.

Perhaps I have chosen poor design, but I am having a difficult time getting
the expectations to function as I understand they should.

In particular, if the method I am testing creates a new object, and then
calls a method with the newly created object, how is the situation properly
mocked?

Here is the code and trace:
public final class MDItemPublisher implements MDItemPublisherI{
 
 private final static Logger log = AtsLogMgr
   .getInstance(MDItemPublisher.class.getPackage().getName());
 
 // JMS iv's
 private final String topicName;
 private final TopicSession topicSession;
 private final TopicPublisher tpub;  

 protected MDItemPublisher(final String topicName, final TopicSession
topicSession,
     final TopicPublisher tpub) {    
   this.topicName = topicName;
   this.topicSession = topicSession;    
   this.tpub = tpub;
 }
 @Override
 public final void publishMD(final MarketDataQuoteI msg) {
   try {
     // get ready to publish
     final ObjectMessage omsg = topicSession.createObjectMessage();
     // set object message to be sent
     omsg.setObject(msg);
     // publish the object message
     tpub.publish(omsg);
   } catch (JMSException e) {
     log.warning(e.getMessage());
   }
 }

 @Override
 public void destroy() {
   try {
     tpub.close();
   } catch (JMSException e) {
     log.warning(e.getMessage());
   }    
 }

 public String toString(){
   StringBuffer sb = new StringBuffer();
   sb.append("MDItemPublisher:" + topicName);
   try {
     sb.append(" delivery mode " + tpub.getDeliveryMode());
     sb.append(" priority: " + tpub.getPriority());
     sb.append(" ttl: " + tpub.getTimeToLive());
     sb.append(" destination: " + tpub.getDestination());
   } catch (JMSException e) {
     log.warning(e.getMessage());
   }
   return sb.toString();
 }

}

public class MDItemPublisherTest {

 private static Mockery context = new JUnit4Mockery();
 @Test
 public final void testMDItemPublisher() {
   String topicName = "testTopicName";
   TopicSession ts = context.mock(TopicSession.class);
   TopicPublisher tpub = context.mock(TopicPublisher.class);
   //execute
   MDItemPublisher underTest = new MDItemPublisher(topicName, ts, tpub);
   //verify
   assertTrue(underTest instanceof MDItemPublisher);
   context.assertIsSatisfied();
 }

 @Test
 public final void testPublishMD() throws JMSException {
   String topicName = "testTopicName";
   final TopicSession ts2 = context.mock(TopicSession.class, "ts2");
   final TopicPublisher tpub2 = context.mock(TopicPublisher.class, "tpub2");
   final ObjectMessage om = context.mock(ObjectMessage.class);
   
   
   MDItemPublisher underTest = new MDItemPublisher(topicName, ts2, tpub2);
   final MarketDataQuoteI testQuote = context.mock(MarketDataQuoteI.class);
   
   context.checking(new Expectations() {{      
       one(ts2).createObjectMessage(testQuote);
                                 
will(returnValue(a(ObjectMessage.class)));
       one (om).setObject(testQuote);
       one (tpub2).publish((Message) testQuote);
     }});
   //execute
   underTest.publishMD(testQuote);
   
   //verify
   context.assertIsSatisfied();
 }

 @Test
 public final void testDestroy() {
   fail("Not yet implemented"); // TODO
 }

}

trace:
java.lang.ClassCastException: $Proxy7
 at
com.ats.bus.md.pub.MDItemPublisherTest$1.<init>(MDItemPublisherTest.java:54)
 at
com.ats.bus.md.pub.MDItemPublisherTest.testPublishMD(MDItemPublisherTest.java:51)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39)
 at
sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke (Method.java:597)
 at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
 at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
 at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
 at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
 at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
 at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
 at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
 at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
 at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
 at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
 at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
 at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
 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)





--
Sent from the jMock - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.