Issue
I am trying to test the following method:
public void transactionToBN() {
List<CustomerDelivery> list = getListOfArticleByStatusA();
...
...
if (!list.isEmpty() || list != null) {
Hashtable<String, Order> docIds = new Hashtable<>();
for (int i = 0; i < list.size(); i++) {
try{
...Some functionalities..
}catch(Throwable t){
countNumId++;
}
}
if (!docIds.isEmpty())
orderTransfer(docIds);
}
}
My test:
@Test
public void canDoTransactionToBN() {
customerDelivery = new CustomerDelivery();
customerDelivery.setCD_COM1("1");
List<CustomerDelivery> customerDeliveries = new ArrayList<>();
customerDeliveries.add(customerDelivery);
Hashtable<String, Order> docIds = new Hashtable<>();
CustomerDeliveryService spy = Mockito.spy(CustomerDeliveryService.class);
//when
Mockito.doReturn(customerDeliveries).when(spy).getListOfArticleByStatusA();
Mockito.doNothing().when(spy).orderTransfer(docIds);
spy.transactionToBN();
Mockito.verify(spy).orderTransfer(docIds);
}
So I am basically just trying to test if orderTransfer() was called. Problem that I run into is that I get a null pointer for a method that is called inside of orderTransfer(), which is something I don't want to test with this unit. I tried .doNothing() so that the code wouldn't keep on going but for some reason it does. Is there something wrong in the way I am trying to verify the methods call or maybe does the code from the whole page, instead of just transactionToBN(), get called?
Solution
It looks like you've only stubbed orderTransfer when it's called with exactly docIds
. I'd assume that it's being called with something that isn't docIds
, so it's still using the un stubbed method.
Try replacing the stub with Mockito.doNothing().when(spy).orderTransfer(any());
There's more detail on argument matchers (any()
) here https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/ArgumentMatchers.html
Answered By - Sam
Answer Checked By - Cary Denson (JavaFixing Admin)