0
@Mock
RefDataCache refDataCache;

TradingAccountEntity tradingAccountEntity = Mockito.mock(TradingAccountEntity.class, Mockito.RETURNS_DEEP_STUBS);
tradingAccountEntity.setMethod1(65);
tradingAccountEntity.setMethod2(65);
Mockito.when(refDataCache.getTradingAccount(x-> x.getMethod1()==65)).thenReturn(tradingAccountEntity);
Mockito.when(tradingAccountEntity.getMethod2()).thenReturn((long) 65);

In the above code, I am getting NullPointerException stating :

java.lang.NullPointerException: Cannot invoke "getMethod1()" because the return value of "getTradingAccount(org.eclipse.collections.api.block.predicate.Predicate)" is null.

I tried changing the code to :

Mockito.when(refDataCache.getTradingAccount(65)).thenReturn(tradingAccountEntity);
Mockito.when(tradingAccountEntity.getMethod2()).thenReturn((long) 65);

Then it shows:

java.lang.NullPointerException: Cannot invoke "getMethod2()" because the return value of "getTradingAccount(org.eclipse.collections.api.block.predicate.Predicate)" is null.

I am not able to understand How should I pass value to getTradingAccount() so that it does not give NullPointerException. Please help.

nehacharya
  • 925
  • 1
  • 11
  • 31
Gazal Jain
  • 39
  • 1
  • 7
  • Can you post the full stacktrace? – m0skit0 Mar 29 '21 at 07:46
  • 13:11:00.746 [main] ERROR PostingService - Error occurred while placing Quote for QuoteRequest: 65 java.lang.NullPointerException: Cannot invoke "refdatacache.TradingAccountEntity.getMethod2()" because the return value of "refdatacache.RefDataCache.getTradingAccount(org.eclipse.collections.api.block.predicate.Predicate)" is null – Gazal Jain Mar 29 '21 at 07:49
  • 2
    `Mockito.when(refDataCache.getTradingAccount(x-> x.getMethod1()==65))` should probably be `Mockito.when(refDataCache.getTradingAccount(any(Predicate.class)))`, you can't really mock lambdas *exactly* because they don't overwrite `equals()` or `hashCode()`, so unless you pass the *exact* same lambda everywhere, your mock method will not match. Futher reading: https://stackoverflow.com/questions/41462913/mockito-verify-that-a-specific-lambda-has-been-passed-as-an-argument-in-mocks-m – Lino Mar 29 '21 at 08:03
  • @Lino Let me try the change you suggested. – Gazal Jain Mar 29 '21 at 08:08

0 Answers0