Glory to Russia
Glory to Russia

Reputation: 18712

What is the analogon of Mockito.spy/doReturn in EasyMock?

Imagine, I have following class:

public class TestClass {
    public class Index<X> {

    }
    public class IndexData {
        private final Index<?> index;

        private final ReentrantReadWriteLock lock =
            new ReentrantReadWriteLock();

        public IndexData(final Index<?> index) {
            super();
            this.index = index;
        }

        public Index<?> getIndex() {
            return index;
        }

        public Lock getReadLock() {
            return lock.readLock();
        }

        public Lock getWriteLock() {
            return lock.writeLock();
        }   
    }

    public void add(final InputClass input)
    {
        final IndexData index = getIndex(input);

        final Lock lock = index.getWriteLock();
        lock.lock();
        try {
            // Do something here, which requires synchronization
        } finally {
            lock.unlock();
        }
    }

    protected IndexData getIndex(final InputClass input) {
        // Some logic of getting the index for input
        return null;
    }
}

I want to write a unit test, which verifies that

  1. in the add method, index.getWriteLock() is used (not index.getReadLock()),
  2. the lock is taken and
  3. released.

Using Mockito I can write a test like this:

@Test
public void testAddUsesWriteLock() {
    // Prepare
    final TestClass objectUnderTest = Mockito.spy(new TestClass());
    final InputClass input = Mockito.mock(InputClass.class);
    final IndexData indexData = Mockito.mock(IndexData.class);
    Mockito.doReturn(indexData).when(objectUnderTest).getIndex(input);
    final Lock lock = Mockito.mock(Lock.class);
    Mockito.doReturn(lock).when(indexData).getWriteLock();

    // Invoke method under test
    objectUnderTest.add(input);

    // Verify
    Mockito.verify(indexData).getWriteLock();
    Mockito.verify(indexData, Mockito.never()).getReadLock();
    Mockito.verify(lock).lock();
    Mockito.verify(lock).unlock();
}

How can I do the same thing with EasyMock?

Concrete: How can I the getIndex method return a mock in EasyMock (line Mockito.doReturn(indexData).when(objectUnderTest).getIndex(input)) ?

Note: You can find the code of this example here .

Upvotes: 11

Views: 6848

Answers (1)

Brad
Brad

Reputation: 15879

In the spirit of providing a possible solution (and contrary to my comments above) you could try one of the following

Option 1

If TestClass implements an interface you could achieve a similar test using andDelegateTo() as described in this post that talks about Easymock not supporting spying

Option 2

Remove your need for spying by extending the TestClass specifically for your testing requirements. This is a common approach to dealing with legacy code bases where you cannot alter the code under test.

I will use Mockito in this example so that it is aligned with your question, however the concept will work the same with Easymock.

public class TestClassUsingMockito {
    /**
    We extend the original class under test so that we can override the creation of IndexData and
    thereby remove the responsibility of creating this object from the @Test method
    */
    private class ClassForTesting extends TestClass {

        private Lock lock;
        private IndexData indexData;

        public ClassForTesting(IndexData indexData, Lock lock) {
            this.indexData = indexData;
            this.lock = lock;
        }

        @Override
        protected IndexData getIndex(InputClass input) {
            return indexData;
        }
    }

    /**
    Look Ma' no more Spys!
    */
    @Test
    public void testAddUsesWriteLock() {        
        // Prepare
        final Lock lock = Mockito.mock(Lock.class);
        final IndexData indexData = Mockito.mock(IndexData.class);
        Mockito.doReturn(lock).when(indexData).getWriteLock();
        // ... now use your new subclass for testing
        final TestClass objectUnderTest = new ClassForTesting(indexData, lock);
        final InputClass input = Mockito.mock(InputClass.class);

        // Invoke method under test
        objectUnderTest.add(input);

        // Verify
        Mockito.verify(indexData).getWriteLock();
        Mockito.verify(indexData, Mockito.never()).getReadLock();
        Mockito.verify(lock).lock();
        Mockito.verify(lock).unlock(); 
    }
}

What is the analogon of Mockito.spy/doReturn in EasyMock?

So having removed the need for a Spy() in your tests the Mockito call

Mockito.doReturn(lock).when(indexData).getWriteLock();

Can be written in EasyMock as

expect(indexData.getWriteLock()).andStubReturn(lock);

EasyMock example of the same Mockito test above

public class TestClassUsingEasymock extends EasyMockSupport {

    private class ClassForTesting extends TestClass {

        private Lock lock;
        private IndexData indexData;

        public ClassForTesting(IndexData indexData, Lock lock) {
            this.indexData = indexData;
            this.lock = lock;
        }

        @Override
        protected IndexData getIndex(InputClass input) {
            return indexData;
        }
    }


    @Test
    public void testAddUsesWriteLock() {
        // Prepare
        final Lock lock = createNiceMock(Lock.class);       
        final IndexData indexData = createNiceMock(IndexData.class);
        EasyMock.expect(indexData.getWriteLock()).andStubReturn(lock);

        // ... now use your new subclass for testing
        final TestClass objectUnderTest = new ClassForTesting(indexData, lock);
        final InputClass input = createNiceMock(InputClass.class);

        lock.lock();
        EasyMock.expectLastCall();

        lock.unlock();
        EasyMock.expectLastCall();

        replayAll();

        // Invoke method under test
        objectUnderTest.add(input);

        // Verify
        verifyAll();
    }
}

Upvotes: 7

Related Questions