想象一下,我有以下课程:
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;
}
}
我想编写一个单元测试,以验证
add
方法中,index.getWriteLock()
被使用(不是index.getReadLock()
),使用Mockito,我可以编写如下测试:
@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();
}
如何使用EasyMock做同样的事情?
具体:我该如何getIndex
在EasyMock(线Mockito.doReturn(indexData).when(objectUnderTest).getIndex(input)
)中返回模拟?
注意:您可以在此处找到此示例的代码。
本着提供可能解决方案的精神(与上述我的评论相反),您可以尝试以下其中一种方法
选项1
如果TestClass
实现了一个接口,则可以使用andDelegateTo()来实现类似的测试,如本博文所述,该博文讨论Easymock不支持间谍程序
选项2
通过扩展TestClass
专门针对您的测试要求的方式来消除间谍活动的需要。这是处理遗留代码库的常用方法,在这些遗留代码库中,您无法更改被测代码。
在本示例中,我将使用Mockito使其与您的问题保持一致,但是该概念与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();
}
}
EasyMock中Mockito.spy / doReturn的类比是什么?
因此,在测试中不再需要Spy()了,Mockito调用
Mockito.doReturn(lock).when(indexData).getWriteLock();
可以用EasyMock编写为
expect(indexData.getWriteLock()).andStubReturn(lock);
上面相同的Mockito测试的EasyMock示例
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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句