Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking twilio with mockito
    text
    copied!<p>I'm writing a unit test for a method what returns true if a sub account is already found on my twilio account. I'm trying to mock this with Mockito but get cast errors casting List to AccountList. I looked through the mockito documentation but might have missed something.</p> <p>This is the test:</p> <pre><code> @Mock TwilioRestClient client; @Mock AccountList accountList; @Mock Iterator&lt;Account&gt; iterator; @Mock Account account; @Test public void testShouldReturnTrueIfAccountNameFound() { final List&lt;Account&gt; list = Arrays.asList(account); when(client.getAccounts()).thenReturn((AccountList) list); when(account.getFriendlyName()).thenReturn("test"); when(accountList.iterator()).thenReturn(list.iterator()); MyTwilioAccountStore store = null; store = new MyTwilioAccountStore(client); Assert.assertTrue(store.subAccountExists("test")); } </code></pre> <p>And this is the method I'm testing. I'm injecting the TwilioRestClient in the constructor.</p> <pre><code> /** * class constructor * * @param client */ public MyTwilioAccountStore(TwilioRestClient client) { fClient = client; } /** * rest client getter * * @return RESTClient */ public TwilioRestClient getRestClient() { return fClient; } /** * Check if a sub account already exists * * @param friendlyName * @return boolean */ public boolean subAccountExists(String friendlyName) { // Build a filter for the AccountList Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); params.put("FriendlyName", friendlyName); AccountList accounts = getRestClient().getAccounts(params); // Loop over accounts // This is where I get NPE for (Account account : accounts) { if (account.getFriendlyName().equalsIgnoreCase(friendlyName)) { return true; } } return false; } </code></pre> <p>This is the getAccounts of the Twilio source code:</p> <pre><code> /** * Get all accounts. For more info: {@link &lt;a * href="http://www.twilio.com/docs/api/rest/account" * &gt;http://www.twilio.com/docs/api/rest/account&lt;/a&gt;} * * @return the list of accounts. */ public AccountList getAccounts() { return this.getAccounts(new HashMap&lt;String, String&gt;()); } </code></pre> <p>How can I mock the AccountList correctly? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload