суббота, 13 апреля 2013 г.

EasyMock

Осмотр одного из замых популярных мокинг-фреймворквов в мире джава:

package com.coreer.training.tdd.mocks.easymock;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;

import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.easymock.IAnswer;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.easymock.samples.ClassUnderTest;
import com.easymock.samples.Collaborator;

/**
 * EasyMock training
 * @author aieremenko
 *
 */
public class EasyMockTest  {
 /**
  * 1. Default mocks - Each method of mocked interface has to be defined obviously, if we want to call them.
  *   Order of calling defined methods does not matter
  * 2. Easy mocks - All method are premocked with default matched values: 0, null
  * 3. Strict mocks - Like default mocks but order has to be the same
  */
 private ClassUnderTest classUnderTest;
 private Collaborator mock;
 
 @Before
 public void setUP() {
  mock = EasyMock.createMock(Collaborator.class);//1
  classUnderTest = new ClassUnderTest();
  classUnderTest.addListener(mock);
 }
 
 @Ignore
 @Test//replay activates mock to be calleable with its defined methods 
 public void testRemoveNonExistingDocument() {
  // 2 we do not expect anything
  EasyMock.replay(mock);
        classUnderTest.removeDocument("Does not exist");
 }
 
 @Test//verify checks if all expected(that we defined) methods were called
 public void testAddDocument() {
  mock.documentAdded("New Document");// 2
  EasyMock.replay(mock);
  classUnderTest.addDocument("New Document", new byte[0]);
  EasyMock.verify(mock);
 }
 
 @Test//verifying example with plenty methods' calls
 public void testAddAndChangeDocument() {
  mock.documentAdded("Document");
        mock.documentChanged("Document");
        mock.documentChanged("Document");
        mock.documentChanged("Document");        
        EasyMock.replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        EasyMock.verify(mock);
 }
 
 @Test//example how to make shorter definition of plural call of the same method
 public void testShorterFormOfSeveralCalls() {
  mock.documentAdded("Document");
        mock.documentChanged("Document");
        EasyMock.expectLastCall().times(3);        
        EasyMock.replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        EasyMock.verify(mock);
 }
 
 @Test//we can define return value of method call
    public void testVoteForRemoval() {
        mock.documentAdded("Document");   // expect document addition
        // expect to be asked to vote for document removal, and vote for it
        EasyMock.expect(mock.voteForRemoval("Document")).andReturn((byte) 42);
        /* 
         * last string can be changed by next two ones:        
         * 
         * mock.voteForRemoval("Document");
         * EasyMock.expectLastCall().andReturn((byte) 42);*/
        
        
        mock.documentRemoved("Document"); // expect document removal
        EasyMock.replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        Assert.assertTrue(classUnderTest.removeDocument("Document"));
        EasyMock.verify(mock);
    }
 

 @Test//except simple types in returns we can define returns as Answer object
 public void testList() {
  List l = EasyMock.createMock(List.class);
  
  EasyMock.expect(l.remove(10)).andAnswer(new IAnswer(){
   public String answer() throws Throwable {
    return EasyMock.getCurrentArguments()[0].toString();
   }
  });
  
  EasyMock.replay(l);
  
  System.out.println(l.remove(10));
  EasyMock.verify(l);
 }
 
 @Test//example of delegation
 public void testListButDelagateToRealImplementation() {
  List l = EasyMock.createMock(List.class);
  
  EasyMock.expect(l.remove(10)).andDelegateTo(new ArrayList(){
   @Override
   public String remove(int index) {
    return Integer.toString(index);
   }
  });
  
  EasyMock.replay(l);
  
  System.out.println(l.remove(10));
  EasyMock.verify(l);
 }
}

Комментариев нет:

Отправить комментарий