使用Jmock测试Webservice的应用

package nl.enovation.commons.monitor;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import javax.xml.bind.JAXBElement;

import nl.enovation.ems.archivewebservice.schema.Message;
import nl.enovation.ems.archivewebservice.schema.MessageInternalIdRquest;
import nl.enovation.ems.archivewebservice.schema.ObjectFactory;

import org.apache.commons.lang.math.RandomUtils;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ws.client.WebServiceFaultException;
import org.springframework.ws.client.core.WebServiceTemplate;


public class MailArchiveWebserviceCheckerTest {

    private MailArchiveWebserviceChecker mailArchiveWebserviceChecker = new MailArchiveWebserviceChecker();

    private WebServiceTemplate webServiceTemplate;

    private ObjectFactory objectFactory;

    private Mockery context = new JUnit4Mockery() {
        {
            setImposteriser(ClassImposteriser.INSTANCE);
        }
    };

    private MessageInternalIdRquest messageInternalIdRquest;

    private Message message;

    @Before
    public void setUp() {
        webServiceTemplate = context.mock(WebServiceTemplate.class);
        objectFactory = new ObjectFactory();
        messageInternalIdRquest = objectFactory.createMessageInternalIdRquest();
        messageInternalIdRquest.setMessageInternalId(createRandomMessageId());
        message = objectFactory.createMessage();
        mailArchiveWebserviceChecker.setWebServiceTemplate(webServiceTemplate);
    }

    @Test
    public void shouldReturnTrue() {
        String messageId = createRandomMessageId();
        message.setMessageId(messageId);
        createMockedResponse();
        assertTrue(mailArchiveWebserviceChecker.check(messageId));
        context.assertIsSatisfied();
    }

    @Test
    public void shouldReturnFalse() {
        String messageId = createRandomMessageId();
        String anotherMessageId = createRandomMessageId();
        message.setMessageId(messageId);
        createMockedResponse();
        assertFalse(mailArchiveWebserviceChecker.check(anotherMessageId));
        context.assertIsSatisfied();
    }
   
    @Test
    public void shouldReturnFalseCausedByException() {
        String messageId = createRandomMessageId();
        String anotherMessageId = createRandomMessageId();
        message.setMessageId(messageId);
        createMockedExceptionResponse();
        assertFalse(mailArchiveWebserviceChecker.check(anotherMessageId));
    }

    private String createRandomMessageId() {
        return String.valueOf(RandomUtils.nextInt());
    }

    private void createMockedResponse() {
        final JAXBElement<Message> retrieveResponseObj = objectFactory
                .createRetrieveMessageThroughMessageIdResponse(message);
        // expectations
        context.checking(new Expectations() {
            {
                one(webServiceTemplate).marshalSendAndReceive(
                        with(any(objectFactory.createMessageInternalIdRquest().getClass())));
                will(returnValue(retrieveResponseObj));
            }
        });
    }

    private void createMockedExceptionResponse() {
        // expectations
        context.checking(new Expectations() {
            {
                one(webServiceTemplate).marshalSendAndReceive(
                        with(any(objectFactory.createMessageInternalIdRquest().getClass())));
                will(throwException(new WebServiceFaultException("")));
               
            }
        });
    }
}


posted on 2008-07-22 10:48 刘铮 阅读(880) 评论(0)  编辑  收藏 所属分类: Jmock


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论