Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 868
Dmitrii Pisarenko Avatar asked Feb 11 '15 07:02

Dmitrii Pisarenko


People also ask

What is doReturn in Mockito?

You can use doReturn-when to specify a return value on a spied object without making a side effect. It is useful but should be used rarely. The more you have a better pattern such as MVP, MVVM and dependency injection, the less chance you need to use Mockito. spy .

What is the difference between EasyMock and Mockito?

Mockito supports both mocks as well as spies. Both spies and mocks perform different functions. Spy creates a partial mock object, whereas mock creates a dummy/ fake (fully mock) object of the real one. Whereas EasyMock supports only mocks.

What is the difference between doReturn and thenReturn in Mockito?

One thing that when/thenReturn gives you, that doReturn/when doesn't, is type-checking of the value that you're returning, at compile time. However, I believe this is of almost no value - if you've got the type wrong, you'll find out as soon as you run your test. I strongly recommend only using doReturn/when .

What is the difference between Mockito and PowerMockito?

While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.


1 Answers

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();
    }
}
like image 178
Brad Avatar answered Oct 07 '22 22:10

Brad