There is an abstract
class
public abstract class BaseProcessor {
public BooksTransaction getBooksTransaction() {
return booksTransaction;
}
}
There is another final class
which is to be tested using Junit
public final class CreateOrganisationProcessor extends BaseProcessor {
public boolean process() throws Exception { //method to be tested
request = new CreateOrganisationRequest(IntegrationSystems.valueOf(getBooksTransaction().getSource()),
IntegrationSystems.valueOf(getBooksTransaction().getDestination()), getBooksTransaction());
request.setRequestTypes(getRequestTypes());
return true;
}
}
I tried spying the BaseProcessor
class and mocking getBooksTransaction
method to return BooksTransaction
Object.
Code:
@Test
public void testProcess() throws Exception {
BaseProcessor spy = Mockito.spy(new CreateOrganisationProcessor());
BooksTransaction booksTransaction = new BooksTransaction();
booksTransaction.setReferenceID(DEFAULT_REFERENCE_ID);
Mockito.doReturn(booksTransaction).when(spy).getBooksTransaction();
}
Here, BooksTransaction
is an JPA Entity
class.
However, when I run the test case, the mock does not seem to be working, it does not return a BooksTransaction
Object.
It neither throws an exception
, nor any error
.
I would like to know the right way to spy
this method so that it returns me an object of BooksTransaction
as per my mock
.
I am new to Mockito
, any help would be appreciable.
Thanks in advance.