使用jMock模拟javax.mail.message

时间:2014-07-15 08:45:00

标签: java unit-testing junit jmock

以下是我试图用来模拟javax.mail.Message的代码。消息实例传递给另一个方法调用getContent(Message message),它返回String实例。该方法的代码也在下面给出。

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()方法代码:

 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;

测试用例因此消息失败。

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)

1 个答案:

答案 0 :(得分:1)

您似乎没有为m1.getContent()

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


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