简体   繁体   English

用jMock模拟javax.mail.message

[英]Mocking javax.mail.message with jMock

Following is the code that I am trying to use for mocking the javax.mail.Message. 以下是我尝试用来模拟javax.mail.Message的代码。 The message instance is passed to another method call getContent(Message message) which returns String instance. 消息实例被传递到另一个方法调用getContent(Message message),该方法返回String实例。 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 : 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;

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)

It seems that you didn't define the expectation for m1.getContent() 似乎您没有定义对m1.getContent()的期望

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM