[SalesForce] Assert Map> has multiple values in list

I have a method that returns:

Map<String, List<Account>> results

I want to have a test class that asserts that the map contains two list records with one string value. I can't figure out how to do the assertion. I know that with a debug that there are two records in the list.

I tried this:

List<Account> outerList = results.values();
System.assertEquals(2, outerList.size());

and I get the error:

Illegal assignment from List<List<Account>> to List<Account>

I tried this:

System.assertEquals(2, results.values().size());

and I get the result:

Assertion Failed: Expected: 2, Actual: 1

I tried this:

 System.assertEquals(2, results.size());

and I get the result:

Assertion Failed: Expected: 2, Actual: 1

What am I doing wrong?

Best Answer

There's only one key, with two elements, so you'd do:

List<List<Account>> accounts = results.values();
System.assertEquals(1, accounts.size(), 'Map should have one key entry');
System.assertEquals(2, accounts[0].size(), 'List should have two accounts in first key');
Related Topic