function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Venkateswarlu PVenkateswarlu P 

Map<String,List<Opportunity>> Test Class

public class Map_Opportunities {
    public Map<string,List<Opportunity>> oppMap        {set;get;}    
    public List<Account> accList                    {set;get;}
    
    public Map_Opportunities(){
        oppMap = new Map<string,List<Opportunity>>();  
        accList=new List<Account>();
        
        accList = [SELECT name,(SELECT Name,stageName FROM Opportunities)FROM Account];
        for (Account a :accList){
            oppMap.put(a.Name, a.Opportunities);
        }                    
    }
}
-------------------------------------------
Test Class
--------------------------------------------
@isTest
    static void mapOpportunits(){
        Map_Opportunities mp = new Map_Opportunities();
        List<Opportunity> optyList = new List<Opportunity>();
        account acc = new Account();
        acc.name = 'Ravi';
        insert acc;
        
        opportunity op = new Opportunity();
        op.Name = 'Xyz';
        op.AccountId = acc.id;
        op.CloseDate = system.today();
        op.StageName ='Closed Won';    
        optyList.add(op);
        insert optyList;
        mp.oppMap.put(acc.Name, optyList);
    }
85% code is covered and Displaying Error : oppMap.put(a.Name, a.Opportunities);
Best Answer chosen by Venkateswarlu P
sfdcMonkey.comsfdcMonkey.com
try this :
 
@isTest
private class testClass{
    static testMethod void TestCls(){ 
        account acc = new account();
          acc.Name = 'test';
         // add all required fields here
        insert acc;
        
        opportunity op = new Opportunity();
        op.Name = 'Xyz';
        op.AccountId = acc.id;
        op.CloseDate = system.today();
        op.StageName ='Closed Won';    
        insert op;
        
	  Map_Opportunities mapOpp = new Map_Opportunities();
        
    }
}
Thanks, let us know if it helps you