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
SakthidasanSakthidasan 

How to cover the Unit Test

I'm new to salesforce.How to write test class for follwing Apex class.it function is get unique ownerId from the object 
 and find out sum of outcome,howmany event occur in the current month and send the email to the owner.
Apex class:
public class oppOwnerOutcome {
    //Remove debug statements
    public void executeLogic(){
        Map<String,List<Meeting__c>> outCmeMap=new Map<String,List<Meeting__c>>();
        Map<String,Decimal> ownerOutComeMap =new Map<String,Decimal>();
        Map<String,String> ownerNameMap =new Map<String,String>();
        Map<String,String> ownerEmailMap =new Map<String,String>(); 
        Set<Id> ownerIdSet = new Set<Id>();     
        for(Meeting__c mData:[SELECT id, OutCome__c, Expected_Close_Date__c, AccountMaster__r.Ownerid, AccountMaster__r.Owner.Name FROM Meeting__c WHERE OutCome_Flag__c = false AND Expected_Close_Date__c = THIS_MONTH]){         
            if(!outCmeMap.containsKey(mData.AccountMaster__r.Owner.id)){                
                List<Meeting__c> meetingList=new List<Meeting__c>();
                meetingList.add(mData);
                outCmeMap.put(mData.AccountMaster__r.Owner.id,meetingList);
            }
            else{
                List<Meeting__c> meetingList=new List<Meeting__c>();
                //Use OwnerId
                meetingList=outCmeMap.get(mData.AccountMaster__r.Owner.id);
                meetingList.add(mData);
                outCmeMap.put(mData.AccountMaster__r.Owner.id,meetingList);
            }
        }
        for(String owrId:outCmeMap.keySet()){
            Decimal totalOutcomePerOwner = 0;
            for(Meeting__c mtg: outCmeMap.get(owrId)){
                totalOutcomePerOwner = mtg.OutCome__c+totalOutcomePerOwner ;
                //System.debug('summary'+res1.OutCome__c);              
            }
            ownerOutComeMap.put(owrId, totalOutComePerOwner);
        }
        
        List<User> owRecList = new List<User>();
        owRecList = [SELECT Id, Name, Email FROM User WHERE Id IN: outCmeMap.keySet()];
        for(User uRec:owRecList){
            ownerNameMap.put(uRec.Id, uRec.Name);
            ownerEmailMap.put(uRec.Id, uRec.Email); 
        }       
        List<EmailObj__c> emailRecList = new List<EmailObj__c>();
        //Move hardcoded values to custom labels or settings
        for(String owrId:outCmeMap.keySet()){
            EmailObj__c emailRec = new EmailObj__c();
            emailRec.send_Address__c = ownerEmailMap.get(owrId);
            String emailBody = 'Dear '+ownerNameMap.get(owrId)+'<br></br>' +
                                'Total Opportunity Outcome expected for this month is '+ownerOutComeMap.get(owrId)+'<br></br>' +
                                'Regards'+'<br></br>' +
                                'Sales Team';
            emailRec.emailBody__c = emailBody;
            emailRecList.add(emailRec);
        }
        //Use database.insert instead of insert statement
        //database.saveResult[] srList = database.insert(emailRecList, false);
        //for(Database.saveResult sr : srList) {
   
        insert emailRecList;
        
        //System.debug('summary:'+summary);
    }         
}
Apex Test Class:
@isTest
private class oppOwnerOutcomeTest {
    
    static  testMethod void testoutCome(){
        
         Account acc=new Account(Name='TestMethod');
        insert acc;
        
        Opportunity opp=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
        insert opp;
        Opportunity opp1=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
        insert opp1;
        
        Meeting__c mRec = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
        insert mRec ;
        Meeting__c mRec1 = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
        insert mRec1 ;
        oppOwnerOutcome tt=new oppOwnerOutcome();
        tt.executeLogic();
        
    }

}
Mahesh DMahesh D
Hi 

Please try the below code:
 
@isTest
private class oppOwnerOutcomeTest {
    
    static testMethod void testoutCome(){
        
        Account acc = new Account(Name='TestMethod');
        insert acc;
        
        Opportunity opp = new Opportunity(Name = 'TestCaseOpp', StageName = 'Prospecting', CloseDate = system.today());
        insert opp;
        Opportunity opp1 = new Opportunity(Name = 'TestCaseOpp',StageName = 'Prospecting', CloseDate = system.today());
        insert opp1;
        
        Meeting__c mRec = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false, Expected_Close_Date__c = System.today());
        insert mRec;
        Meeting__c mRec1 = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false, Expected_Close_Date__c = System.today());
        insert mRec1;
        oppOwnerOutcome tt = new oppOwnerOutcome();
        tt.executeLogic();
    }
}

Regards,
Mahesh
SakthidasanSakthidasan
you're put my same code.it doesn't work
Mahesh DMahesh D
I changed it by adding the Expected_Close_Date__c while inserting Meeting__c record.

Please let me know what is the actual issue you are getting for your test class.

Regards,
Mahesh
SakthidasanSakthidasan
How to test containsKey method in test class?
Mahesh DMahesh D
If it enters into the for loop, it will automatically test that.

After copying my code till what line the code got covered?

Also provide the % code coverage.

Regards,
Mahesh
SakthidasanSakthidasan
it didn't check if condition.ti covers 41%
Mahesh DMahesh D
Please paste/print screen the Code Coverage lines from Developer console.

Regards,
Mahesh
Mahesh DMahesh D
Remove this condition Expected_Close_Date__c = THIS_MONTH from the SOQL query, just for testing and see how it is working.

Also paste the latest Test Class.

Regards,
Mahesh