Bilbo Baggins
Bilbo Baggins

Reputation: 3019

Mocking javax.mail.message with jMock

Following is the code that I am trying to use for mocking the javax.mail.Message. The message instance is passed to another method call getContent(Message message) which returns String instance. The code for this method is also given below.

Calendar cal = Calendar.getInstance();
mockery.setImposteriser(ClassImposteriser.INSTANCE);
final Message[] messages;
List<Message> ms = new ArrayList<Message>();
ms.add(mockery.mock(Message.class, "m1"));
ms.add(mockery.mock(Message.class, "m3"));
messages = ms.toArray(new Message[10]);

mockery.checking(new Expectations() {
    {
        allowing(messages[0]).getContentType();
        will(returnValue("text/plain"));

        allowing(messages[1]).getContentType();
        will(returnValue("html"));

    }
});
String contentM1 = getPasswordResetJob().getContent(messages[0]);
assertEquals("text/plain", contentM1);

getContent() method code :

 log.info("Message content type::" + message.getContentType());
    if (message.getContentType().contains("text/plain;") 
        && message.getContent() != null) {
        content = message.getContent().toString();
    } else {
        content = getMultipartContent(message);
    }
    return content;

The test case fails with this message.

unexpected invocation: m1.getContent()
expectations:
  allowed, already invoked 2 times: m1.getContentType(); returns "text/plain"
  allowed, never invoked: m3.getContentType(); returns "html"
    at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
    at org.jmock.Mockery.dispatch(Mockery.java:204)
    at org.jmock.Mockery.access$000(Mockery.java:37)
    at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
    at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
    at org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
    at org.jmock.lib.legacy.ClassImposteriser$4.invoke(ClassImposteriser.java:137)
    at $javax.mail.Message$$EnhancerByCGLIB$$8abdf7fe.getContent(<generated>)
    at com.abc.scheduler.AbstractSchedulerJob.getMultipartContent(AbstractSchedulerJob.java:222)
    at com.abc.scheduler.AbstractSchedulerJob.getContent(AbstractSchedulerJob.java:151)
    at com.abc.scheduler.PasswordResetJobTest.testCreateAlertList(PasswordResetJobTest.java:127)
    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 junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    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:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Upvotes: 0

Views: 231

Answers (1)

Yugang Zhou
Yugang Zhou

Reputation: 7283

It seems that you didn't define the expectation for m1.getContent()

if (message.getContentType().contains("text/plain;") 
    && message.getContent() != null) {


    content = message.getContent().toString();//message.getContent() is not defined

Upvotes: 1

Related Questions