In my implementation class I have a read write lock definition as follows,
@Inject
@Named("CustomizedLock")
private ReentrantReadWriteLock rwLock;
I'm using it in a method named run()
as,
rwLock.writeLock().lock();
to lock the process. But when I'm trying to test this with mockito I found ReentrantReadWriteLock
is initialized. But when I'm trying to get rwLock.writeLock()
it's null. Here is my test.
@Mock
private ReentrantReadWriteLock feedReadWriteLock;
@InjectMocks
private CustomModule module = mock(CustomModule.class);
/////////////////////////
@Test
public void test() {
when(module.getReadWriteLock()).thenReturn(mock(ReentrantReadWriteLock.class));
PowerMockito.doReturn(new ReentrantReadWriteLock()).when(module.getReadWriteLock());
cacheJob.run();
}
As I said rwLock.writeLock()
is null but rwLock is initialized. Please explain how this happens with mockito. And what is the ideal way to do this?