Reputation: 18712
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
add
method, index.getWriteLock()
is used (not index.getReadLock()
),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
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