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
Padmini S 26Padmini S 26 

increase apex class code coverage

Hi,

I have written test class for below apex class. Code coverage is 25% only.
public class OpportunityTriggerHelper_AOPLine{

    // this function use for update aop line record called when opprtunity status closed .
    public void updateAOPLineOnOppClosed(){
        List<Opportunity> newOpportunities = (List<Opportunity>)trigger.new;
        Map<Id,Opportunity> oldMapOpportunities = (Map<Id,Opportunity>)trigger.oldMap;
        Map<Id,Opportunity> newMapOpportunities =(Map<Id,Opportunity>)trigger.newMap;
        
        List<Id> oppIds = new List<Id>();
 for(Opportunity opp : newOpportunities ){
            if(oldMapOpportunities.get(opp.Id).StageName != opp.StageName && opp.StageName == 'Closed' && opp.Month_sap__c != null && opp.Month__c != null && opp.Financial_Year__c!= null)
                oppIds.add(opp.Id);            
        }  
        if(oppIds.size() > 0){
            Set<Id> useridSet = new Set<Id>();
            Set<String> oppMonths = new Set<String>();
            Set<String> oppYears = new Set<String>();
            List<Opportunity> oppList = [SELECT Id,Account.Category__c,MTD_Volume__c,MTD_Focussed_Volume__c,MTD_GP__c,Month__c,Year__c,Financial_Year__c,OwnerId,Owner.ManagerId,Owner.Manager.ManagerId,(SELECT Id, UserId,User.ManagerId,User.Manager.ManagerId,TeamMemberRole 
 FROM OpportunityTeamMembers WHERE TeamMemberRole != null AND UserId != null)  FROM Opportunity WHERE Id IN:oppIds];
            for(Opportunity opp : oppList){
                oppMonths.add(opp.Month__c);
                oppYears.add(opp.Financial_Year__c);
                if(opp.OwnerId != null)
                    useridSet.add(opp.OwnerId);
                 if(opp.Owner.ManagerId != null)
                    useridSet.add(opp.Owner.ManagerId);
              for(OpportunityTeamMember oppTemMember : opp.OpportunityTeamMembers){
                    useridSet.add(oppTemMember.UserId);
                     if(oppTemMember.User.ManagerId != null)
                        useridSet.add(oppTemMember.User.ManagerId);
                     if(oppTemMember.User.Manager.ManagerId != null)
                        useridSet.add(oppTemMember.User.Manager.ManagerId);
                }
           }
           
           if(useridSet.size() > 0 && oppMonths.size() >0 && oppYears.size() >0){
                Map<Id,User> userMap = new Map<Id,User>([SELECT Id,Profile.Name FROM User WHERE Id IN: useridSet]);
              Map<String,Id> aopLnMap = new Map<String,Id>();
                for(AOP_Line__c aopLn : [SELECT Id,Month__c,MTD_Regional_Vol_in_kL__c, MTD_National_Vol_in_kL__c,MTD_Focus_Vol_in_kL__c, MTD_Regional_GP__c, MTD_National_GP__c,                                            MTD_New_Win_in_kL__c,Valvoline_Financial_Year__c,AOP_Plan_User__c FROM AOP_Line__c WHERE  AOP_Plan_User__c IN : useridSet AND Month__c IN : oppMonths AND Valvoline_Financial_Year__c IN : oppYears]){
                  String keyMap = aopLn.Month__c.toUpperCase() +'#'+aopLn.Valvoline_Financial_Year__c+'#'+aopLn.AOP_Plan_User__c;   
                    aopLnMap.put(keyMap,aopLn.Id);
               }
               
               if(aopLnMap.size() > 0){
                   List<Monthly_line_Logs__c> inserMonthLn = new List<Monthly_line_Logs__c>();
                   for(Opportunity opp : oppList){
opp.MTD_Volume__c = opp.MTD_Volume__c != null ?opp.MTD_Volume__c : 0;
opp.MTD_GP__c = opp.MTD_GP__c != null ? opp.MTD_GP__c : 0;
opp.MTD_Focussed_Volume__c = opp.MTD_Focussed_Volume__c != null ? opp.MTD_Focussed_Volume__c : 0;
String keyMap = opp.Month__c.toUpperCase() +'#'+opp.Financial_Year__c;
        if(opp.OwnerId != null){
                            if(aopLnMap.containsKey(keyMap+'#'+opp.OwnerId)){
                             Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
   mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+opp.OwnerId);
          mnthAopLn.MTD_Regional_Vol_in_kL__c = opp.MTD_Volume__c;
           mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;                         
                                mnthAopLn.MTD_Regional_GP__c = opp.MTD_GP__c;
                    if(opp.Account.Category__c== 'New')
            mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
              inserMonthLn.add(mnthAopLn);
                            }
                        }
                        //M1 : TBM's Manager
                        if(opp.Owner.ManagerId != null){
                            if(aopLnMap.containsKey(keyMap+'#'+opp.Owner.ManagerId)){
                                if(userMap.get(opp.Owner.ManagerId).profile.name.equalsIgnoreCase('ZM')){
   Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+opp.Owner.ManagerId);
  mnthAopLn.MTD_Regional_Vol_in_kL__c = opp.MTD_Volume__c;
 mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;                         
                                    mnthAopLn.MTD_Regional_GP__c = opp.MTD_GP__c;
                                    if(opp.Account.Category__c== 'New')
          mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
                                inserMonthLn.add(mnthAopLn); 
                                }else if(userMap.get(opp.Owner.ManagerId).profile.name.equalsIgnoreCase('NSH')){
Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
       mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+opp.Owner.ManagerId);
                                  mnthAopLn.MTD_National_Vol_in_kL__c = opp.MTD_Volume__c;
                    mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;                         
                                    mnthAopLn.MTD_National_GP__c = opp.MTD_GP__c;
                                    if(opp.Account.Category__c== 'New')
                                   mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
                                    inserMonthLn.add(mnthAopLn); 
                                }                                  
                            }
                        }
                    
                        if(opp.Owner.Manager.ManagerId != null){
                        if(aopLnMap.containsKey(keyMap+'#'+opp.Owner.Manager.ManagerId)){                                                                if(userMap.get(opp.Owner.Manager.ManagerId).profile.name.equalsIgnoreCase('NSH')){
 Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+opp.Owner.Manager.ManagerId);
mnthAopLn.MTD_National_Vol_in_kL__c = opp.MTD_Volume__c;
mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;                         
                                    mnthAopLn.MTD_National_GP__c = opp.MTD_GP__c;
                                   if(opp.Account.Category__c== 'New')
 mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
                                  inserMonthLn.add(mnthAopLn); 
                                }                             
                            }
                        }
                        
 for(OpportunityTeamMember oppTemMember : opp.OpportunityTeamMembers){
                            if(oppTemMember.TeamMemberRole.EqualsIgnoreCase('RKAM')){       
                                if(aopLnMap.containsKey(keyMap+'#'+oppTemMember.UserId)){
Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+oppTemMember.UserId);
mnthAopLn.MTD_National_Vol_in_kL__c = opp.MTD_Volume__c;
mnthAopLn.MTD_National_GP__c = opp.MTD_GP__c;
mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;
 if(opp.Account.Category__c== 'New')
mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
  inserMonthLn.add(mnthAopLn);                           
                                }
   if(oppTemMember.User.ManagerId != null && oppTemMember.User.ManagerId != opp.Owner.ManagerId){                                   if(aopLnMap.containsKey(keyMap+'#'+oppTemMember.User.ManagerId)){                                      if(userMap.get(oppTemMember.User.ManagerId).profile.name.equalsIgnoreCase('ZM')){
 Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+oppTemMember.User.ManagerId);
       mnthAopLn.MTD_National_Vol_in_kL__c = opp.MTD_Volume__c;
          mnthAopLn.MTD_National_GP__c = opp.MTD_GP__c;
           mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;             
          if(opp.Account.Category__c== 'New')
          mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
                                            inserMonthLn.add(mnthAopLn);
                                        }   
                                    }                    
                                } 
                            }else if(oppTemMember.TeamMemberRole.EqualsIgnoreCase('NKAM')){
                                if(aopLnMap.containsKey(keyMap+'#'+oppTemMember.UserId)){                                     Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+oppTemMember.UserId);
 mnthAopLn.MTD_National_Vol_in_kL__c = opp.MTD_Volume__c;
  mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;
 mnthAopLn.MTD_National_GP__c = opp.MTD_GP__c;
      if(opp.Account.Category__c== 'New')
 mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
                                    inserMonthLn.add(mnthAopLn);                             
                                } 
                            }else if(oppTemMember.TeamMemberRole.EqualsIgnoreCase('Segment Manager')){
                                if(aopLnMap.containsKey(keyMap+'#'+oppTemMember.UserId)){
 Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+oppTemMember.UserId);
mnthAopLn.MTD_National_Vol_in_kL__c = opp.MTD_Volume__c;
mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;
                                    mnthAopLn.MTD_National_GP__c = opp.MTD_GP__c;
                                    
                                    if(opp.Account.Category__c== 'New')
        mnthAopLn.MTD_New_Win_in_kL__c = opp.MTD_Volume__c;
                                    inserMonthLn.add(mnthAopLn);                              
                                } 
                            }else if(oppTemMember.TeamMemberRole.EqualsIgnoreCase('Product Manager')){
                                if(aopLnMap.containsKey(keyMap+'#'+oppTemMember.UserId)){
Monthly_line_Logs__c mnthAopLn = new Monthly_line_Logs__c(Monthly_Opportunity__c = opp.Id); 
  mnthAopLn.Monthly_line__c = aopLnMap.get(keyMap+'#'+oppTemMember.UserId);
  mnthAopLn.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;
                           inserMonthLn.add(mnthAopLn);                     
                                }  }  } }
 if(inserMonthLn.size() > 0)
                       insert inserMonthLn;
               }
             }} }}
Test Class:
==========
@isTest
public class OpportunityTriggerHelper_AOPLineTest1 {
     static testMethod void AOPTest1(){        
        List<Opportunity> oppLst= new List<Opportunity>();        
        profile p1 = [SELECT Id FROM Profile WHERE Name='NSH']; 
        User u= new User();
            u.FirstName ='Sagar';
            u.Username='Sagar3443@acme.com';
            u.LastName='Sagar';
            u.Email='Sagar343@abc.com';
            u.Alias='Sagar34';
            u.CommunityNickname='Sagar';
            u.TimeZoneSidKey='America/Los_Angeles';
            u.LocaleSidKey='en_US';
            u.EmailEncodingKey='UTF-8';
            u.ProfileId = p1.Id;
            u.LanguageLocaleKey='en_US';
           insert u;
         profile p3 = [SELECT Id FROM Profile WHERE Name='ZM']; 
        User uzm= new User();
            uzm.FirstName ='Sagar243';
            uzm.Username='Sagar23@acme.com';
            uzm.LastName='Saga3r';
            uzm.Email='Sagar34342@abc.com';
            uzm.Alias='Sagar234';
            uzm.CommunityNickname='Sagar23';
            uzm.TimeZoneSidKey='America/Los_Angeles';
            uzm.LocaleSidKey='en_US';
            uzm.EmailEncodingKey='UTF-8';
            uzm.ProfileId = p3.Id;
            uzm.LanguageLocaleKey='en_US';
            insert uzm;
        profile p2 = [SELECT Id FROM Profile WHERE Name='TBM']; 
        User ut= new User();
            ut.FirstName ='Sagar243';
            ut.Username='Sagar23434@acme.com';
            ut.LastName='Saga334r';
            ut.Email='Sagar34342@abc.com';
            ut.Alias='Sagar234';
            ut.CommunityNickname='Sagar2';
            ut.TimeZoneSidKey='America/Los_Angeles';
            ut.LocaleSidKey='en_US';
            ut.EmailEncodingKey='UTF-8';
            ut.ProfileId = p2.Id;
            ut.LanguageLocaleKey='en_US';
            ut.ManagerId=uzm.Id;
            insert ut;
List<Account> accLst= new List<Account>();
String recordTypeSAP  = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('SAP Order').getRecordTypeId();
        Account acc= new Account();
         acc.Name='Testclass acc1';
        acc.Is_Parent__c='N';
        acc.Type='Factory Fill';
        acc.Indutry_Segment__c='On-Highway';
        acc.Market_Segment__c='PowerGen';
        acc.Quote_allowed__c='Yes';
        acc.BU_Name__c='OEM';
        acc.Category__c='New';
         acc.Account_Type__c='Factory Fill';
        accLst.add(acc);
         insert accLst;
         Opportunity opp = new Opportunity();
        opp.RecordTypeId = recordTypeSAP;
        opp.AccountId=acc.Id;
        opp.Name='Testclass Opp SAP';
        opp.StageName='Open';   
        opp.Month__c='OCT';
        opp.Year__c='2017';
        opp.Financial_Year__c='2017';
        opp.Month_sap__c='OCT';
        opp.Calendar_Year__c ='2017';
        opp.closeDate = System.today();
        opp.ownerId = ut.Id;
       // opp.Owner.Manager.ManagerId = ut.ManagerId;
        opp.MTD_Volume__c = 100;
        opp.MTD_GP__c = 50;
        opp.MTD_Focussed_Volume__c = 100;
         insert opp;
     
    List<OpportunityTeamMember> oppTMLst= new List<OpportunityTeamMember>();    
        OpportunityTeamMember ptm= new OpportunityTeamMember();
        ptm.TeamMemberRole = 'Zonal Manager';
        ptm.OpportunityId = opp.Id;
        ptm.OpportunityAccessLevel = 'Edit';
        ptm.UserId=uzm.Id;
        oppTMLst.add(ptm);
        
         OpportunityTeamMember ptm2= new OpportunityTeamMember();
        ptm2.TeamMemberRole = 'NSH';
        ptm2.OpportunityId = opp.Id;
        ptm2.OpportunityAccessLevel = 'Edit';
        ptm2.UserId=u.Id;
        oppTMLst.add(ptm2);
        
        OpportunityTeamMember ptm3= new OpportunityTeamMember();
        ptm3.TeamMemberRole = 'TBM';
        ptm3.OpportunityId = opp.Id;
        ptm3.OpportunityAccessLevel = 'Edit';
        ptm3.UserId=uzm.Id;
        oppTMLst.add(ptm3);
        insert oppTMLst;
       List<AOP__c> aopList = new List<AOP__c>();
        AOP__c aop = new AOP__c();
        aop.AOP_DSO__c = 52.00;
        aop.Valvoline_Financial_Year__c = '2017';
        aop.AOP_Regional_Vol_in_kL__c = 1.00;
        aop.AOP_National_Vol_in_kL__c = 2.00;
        aop.AOP_Focus_Vol_in_kL__c = 1.00;
        aop.AOP_Regional_GP__c = 1;
        aop.AOP_Forecast_Accuracy__c =10;
        aop.AOP_New_Win_in_kL__c = 3.00;
        aop.AOP_National_GP__c = 1.00;    
        aopList.add(aop);
         insert aopList;
         List<AOP_Line__c> AopLineList = new List<AOP_Line__c>();
        AOP_Line__c aopLine3 = new AOP_Line__c();
       aopLine3.AOP__c = aop.Id;
       aopLine3.AOP_Plan_User__c=uzm.Id;
       aopLine3.AOP_Regional_Vol_in_kL__c = aop.AOP_Regional_Vol_in_kL__c;
       aopLine3.AOP_New_Win_in_kL__c = aop.AOP_New_Win_in_kL__c;
       aopLine3.AOP_DSO__c = aop.AOP_DSO__c;
       aopLine3.AOP_Forecast_Accuracy__c = aop.AOP_Forecast_Accuracy__c;
       aopLine3.AOP_National_Vol_in_kL__c = aop.AOP_National_Vol_in_kL__c;
       aopLine3.AOP_Regional_GP__c = aop.AOP_Regional_GP__c;
       aopLine3.AOP_National_GP__c = aop.AOP_National_GP__c;
       aopLine3.AOP_Focus_Vol_in_kL__c = aop.AOP_Focus_Vol_in_kL__c;
       aopLine3.Month__c = 'Oct';
       AopLineList.add(aopLine3);
       List<Monthly_Line_Logs__c> molList = new List<Monthly_Line_Logs__c>();
       Monthly_line_Logs__c mon1 = new Monthly_line_Logs__c();
                mon1.Monthly_line__c= opp.OwnerId;
                mon1.Monthly_Opportunity__c= opp.Id;
                mon1.MTD_Regional_Vol_in_kL__c = opp.MTD_Volume__c;
                mon1.MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c;
                mon1.MTD_Regional_GP__c = opp.MTD_GP__c;
                mon1.MTD_New_Win_in_kL__c = opp.MTD_Volume__c ; 
                molList.add(mon1); 
                test.startTest();
       try{
               
             insert AopLineList;    
              insert molList;   
                
          }
       catch(Exception E){}
     
           opp.StageName='Closed';
           update opp;       
       test.stopTest();
    }}
Can anyone help on this how to increase the code coverage and where i am missing.

Thanks in Advance.  
Glyn Anderson 3Glyn Anderson 3
Padmini,  Sometimes it easier to increase code coverage by changing the code you're testing.  I have refactored your class to make it easier to read, maintain, and test.  I moved all the repetitive Montly Line Log code into an inner class called, "MonthlyLineLogManager".  This cleaned up the main method by moving big chunks of code into the manager's methods.  I use early "return" statements to reduce the indentation levels.  I manipulated the boolean logic to reduce the number of code paths through the method.  I haven't changed what the test code does yet, but I bet that running the same test on the new class will produce a much higher code coverage percentage.  Please save your original code before replacing it with my version.  I have no way to test what I wrote, so you will have to check it thoroughly.  There may be typos that prevent it from compiling as is.  I'm pretty confident that it does exactly what your original does.  Let me know how far we got with this, and if you need any additional help.

<pre>
public class OpportunityTriggerHelper_AOPLine
{
    // this function use for update aop line record called when opprtunity status closed
    public void updateAOPLineOnOppClosed()
    {
        List<Opportunity> newOpportunities = (List<Opportunity>) Trigger.new;
        Map<Id,Opportunity> oldMapOpportunities = (Map<Id,Opportunity>) Trigger.oldMap;
        Map<Id,Opportunity> newMapOpportunities = (Map<Id,Opportunity>) Trigger.newMap;
         
        List<Id> oppIds = new List<Id>();
        for ( Opportunity opp : newOpportunities )
        {
            if  ( oldMapOpportunities.get( opp.Id ).StageName != opp.StageName
                &&  opp.StageName == 'Closed'
                &&  opp.Month_sap__c != null
                &&  opp.Month__c != null
                &&  opp.Financial_Year__c != null
                ) oppIds.add( opp.Id );
        }
        if ( oppIds.isEmpty() ) return;

        List<Opportunity> oppList =
        [   SELECT  Id, Account.Category__c, MTD_Volume__c, MTD_Focussed_Volume__c,
                    MTD_GP__c,Month__c, Year__c, Financial_Year__c, OwnerId,
                    Owner.ManagerId, Owner.Manager.ManagerId,
                (   SELECT  Id, UserId, User.ManagerId, User.Manager.ManagerId, TeamMemberRole
                    FROM    OpportunityTeamMembers
                    WHERE   (   TeamMemberRole != null
                            AND UserId != null
                            )
                )
                    FROM    Opportunity
                    WHERE   Id IN :oppIds
        ];

        Set<Id> useridSet = new Set<Id>();
        Set<String> oppMonths = new Set<String>();
        Set<String> oppYears = new Set<String>();

        for ( Opportunity opp : oppList )
        {
            oppMonths.add( opp.Month__c );
            oppYears.add( opp.Financial_Year__c );
            if ( opp.OwnerId != null ) useridSet.add( opp.OwnerId );
            if ( opp.Owner.ManagerId != null ) useridSet.add( opp.Owner.ManagerId );
            for ( OpportunityTeamMember oppTemMember : opp.OpportunityTeamMembers )
            {
                useridSet.add( oppTemMember.UserId );
                if ( oppTemMember.User.ManagerId != null ) useridSet.add( oppTemMember.User.ManagerId );
                if ( oppTemMember.User.Manager.ManagerId != null ) useridSet.add( oppTemMember.User.Manager.ManagerId );
            }
        }
        if ( useridSet.isEmpty() || oppMonths.isEmpty() || oppYears.isEmpty() ) return;

        Map<Id,User> userMap = new Map<Id,User>( [SELECT Id, Profile.Name FROM User WHERE Id IN :useridSet] );

        MonthlyLineLogManager mllMgr = new MonthlyLineLogManager();

        for ( AOP_Line__c aopLn :
            [   SELECT  Id, Month__c, MTD_Regional_Vol_in_kL__c, MTD_National_Vol_in_kL__c,
                        MTD_Focus_Vol_in_kL__c, MTD_Regional_GP__c, MTD_National_GP__c,
                        MTD_New_Win_in_kL__c, Valvoline_Financial_Year__c, AOP_Plan_User__c
                FROM    AOP_Line__c
                WHERE   (   AOP_Plan_User__c IN :useridSet
                        AND Month__c IN :oppMonths
                        AND Valvoline_Financial_Year__c IN :oppYears
                        )
            ]
            )
        {
            mllMgr.addAOPLine( aopLn );
        }
        if ( mllMgr.isEmpty() ) return;

        for ( Opportunity opp : oppList )
        {
            if ( opp.MTD_Volume__c == null ) opp.MTD_Volume__c = 0;
            if ( opp.MTD_GP__c == null ) opp.MTD_GP__c = 0;
            if ( opp.MTD_Focussed_Volume__c == null ) opp.MTD_Focussed_Volume__c = 0;

            if ( opp.OwnerId != null ) 
            {
                mllMgr.addMonthlyLineLog( opp, opp.OwnerId );
            }

            if  (   opp.Owner.ManagerId != null
                &&  (   userMap.get( opp.Owner.ManagerId ).Profile.Name == 'ZM'
                    ||  userMap.get( opp.Owner.ManagerId ).Profile.Name == 'NSH'
                    )
                )
            {
                mllMgr.addMonthlyLineLog( opp, opp.Owner.ManagerId );
            }
         
            if  (   opp.Owner.Manager.ManagerId != null
                &&  userMap.get( opp.Owner.Manager.ManagerId ).Profile.Name == 'NSH'
                )
            {
                mllMgr.addMonthlyLineLog( opp, opp.Owner.Manager.ManagerId );
            }
             
            for ( OpportunityTeamMember oppTemMember : opp.OpportunityTeamMembers )
            {
                if  (   oppTemMember.TeamMemberRole == 'RKAM'
                    ||  oppTemMember.TeamMemberRole == 'NKAM'
                    ||  oppTemMember.TeamMemberRole == 'Segment Manager'
                    )
                {
                    mllMgr.addMonthlyLineLog( opp, oppTemMember.UserId );
                }

                if ( oppTemMember.TeamMemberRole == 'Product Manager' )
                {
                    mllMgr.addMonthlyLineLogPM( opp, oppTemMember.UserId );
                }

                if  (   oppTemMember.TeamMemberRole == 'RKAM'
                    &&  oppTemMember.User.ManagerId != null
                    &&  oppTemMember.User.ManagerId != opp.Owner.ManagerId
                    &&  userMap.get( oppTemMember.User.ManagerId ).Profile.Name == 'ZM'
                    )
                {
                    mllMgr.addMonthlyLineLog( opp, oppTemMember.User.ManagerId );
                }
            }
        }
        mllMgr.insertMLLs();
    }

    private class MonthlyLineLogManager
    {
        Map<String,Id> aopLnMap = new Map<String,Id>();
        List<Monthly_line_Logs__c> inserMonthLn = new List<Monthly_line_Logs__c>();

        String keyMap( String a, String b, String c ) { return a + '#' + b + '#' + c; }

        String keyMap( AOP_Line__c aopLn )
        {
            return keyMap
            (   aopLn.Month__c.toUpperCase()
            ,   aopLn.Valvoline_Financial_Year__c
            ,   aopLn.AOP_Plan_User__c
            );
        }

        String keyMap( Opportunity opp, Id keyId )
        {
            return keyMap
            (   opp.Month__c.toUpperCase()
            ,   opp.Financial_Year__c
            ,   keyId
            );
        }

        void addAOPLine( AOP_Line__c aopLn )
        {
            aopLnMap.put( keyMap( aopLn ), aopLn.Id );
        }

        Boolean isEmpty() { return aopLnMap.isEmpty(); }

        void addMonthlyLineLog( Opportunity opp, Id keyId )
        {
            String key = keyMap( opp, keyId );
            if ( !aopLnMap.containsKey( key ) ) return;

            inserMonthLn.add
            (   new Monthly_line_Logs__c
                (   Monthly_Opportunity__c = opp.Id
                ,   Monthly_line__c = aopLnMap.get( key )
                ,   MTD_National_Vol_in_kL__c = opp.MTD_Volume__c
                ,   MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c
                ,   MTD_National_GP__c = opp.MTD_GP__c
                ,   MTD_New_Win_in_kL__c = opp.Account.Category__c == 'New' ? opp.MTD_Volume__c : null
                )
            );
        }

        void addMonthlyLineLogPM( Opportunity opp, Id keyId )
        {
            String key = keyMap( opp, keyId );
            if ( !aopLnMap.containsKey( key ) ) return;

            inserMonthLn.add
            (   new Monthly_line_Logs__c
                (   Monthly_Opportunity__c = opp.Id
                ,   Monthly_line__c = aopLnMap.get( key )
                ,   MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c
                )
            );
        }

        void insertMLLs()
        {
            insert inserMonthLn;
        }
    }
}


Test Class:
==========
@isTest
public class OpportunityTriggerHelper_AOPLineTest1
{
    private static User newUser( Profile profile, String name, User manager )
    {
        return new User
        (   ProfileId = profile.Id
        ,   FirstName = name
        ,   LastName = name
        ,   Email = name + '@abc.com'
        ,   Username = name + '@acme.com'
        ,   Alias = name
        ,   CommunityNickname = name
        ,   TimeZoneSidKey = 'America/Los_Angeles'
        ,   LocaleSidKey = 'en_US'
        ,   EmailEncodingKey = 'UTF-8'
        ,   LanguageLocaleKey = 'en_US'
        ,   ManagerId = manager != null ? manager.Id : null
        );
    }

    static testMethod void AOPTest1()
    {
        List<Opportunity> oppLst = new List<Opportunity>();

        Profile profileNSH = [SELECT Id FROM Profile WHERE Name = 'NSH'];
        profile profileTBM = [SELECT Id FROM Profile WHERE Name='TBM'];
        profile profileZM = [SELECT Id FROM Profile WHERE Name='ZM'];

        System.runAs( [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()] )
        {
            User userNSH = newUser( profileNSH, 'SagarNSH', null );
            insert userNSH;

            User userZM = newUser( profileZM, 'SagarZM', null );
            insert userZM;

            User userTBM = newUser( profileTBM, 'SagarTBM', userTBM );
            insert userTBM;
        }

        Account acc = new Account
        (   Name = 'Testclass acc1'
        ,   Is_Parent__c = 'N'
        ,   Type = 'Factory Fill'
        ,   Indutry_Segment__c = 'On-Highway'
        ,   Market_Segment__c = 'PowerGen'
        ,   Quote_allowed__c = 'Yes'
        ,   BU_Name__c = 'OEM'
        ,   Category__c = 'New'
        ,   Account_Type__c = 'Factory Fill'
        );
        insert acc;

        String recordTypeSAP = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('SAP Order').getRecordTypeId();

        Opportunity opp = new Opportunity
        (
        ,   RecordTypeId = recordTypeSAP
        ,   AccountId = acc.Id
        ,   Name = 'Testclass Opp SAP'
        ,   StageName = 'Open'; 
        ,   Month__c = 'OCT'
        ,   Year__c = '2017'
        ,   Financial_Year__c = '2017'
        ,   Month_sap__c = 'OCT'
        ,   Calendar_Year__c = '2017'
        ,   closeDate = System.today()
        ,   ownerId = userTBM.Id
        ,   MTD_Volume__c = 100
        ,   MTD_GP__c = 50
        ,   MTD_Focussed_Volume__c = 100
        );
        insert opp;
      
        insert new List<OpportunityTeamMember>
        {   new OpportunityTeamMember
            (   TeamMemberRole = 'Zonal Manager'
            ,   OpportunityId = opp.Id
            ,   OpportunityAccessLevel = 'Edit'
            ,   UserId=userZM.Id
            )
        ,   new OpportunityTeamMember
            (   TeamMemberRole = 'NSH'
            ,   OpportunityId = opp.Id
            ,   OpportunityAccessLevel = 'Edit'
            ,   UserId = userNSH.Id
            )
        ,   new OpportunityTeamMember
            (   TeamMemberRole = 'TBM'
            ,   OpportunityId = opp.Id
            ,   OpportunityAccessLevel = 'Edit'
            ,   UserId = userZM.Id
            )
        };

        AOP__c aop = new AOP__c
        (   AOP_DSO__c = 52.00
        ,   Valvoline_Financial_Year__c = '2017'
        ,   AOP_Regional_Vol_in_kL__c = 1.00
        ,   AOP_National_Vol_in_kL__c = 2.00
        ,   AOP_Focus_Vol_in_kL__c = 1.00
        ,   AOP_Regional_GP__c = 1
        ,   AOP_Forecast_Accuracy__c =10
        ,   AOP_New_Win_in_kL__c = 3.00
        ,   AOP_National_GP__c = 1.00
        );
        insert aop;

        AOP_Line__c aopLine3 = new AOP_Line__c
        (   AOP__c = aop.Id
        ,   AOP_Plan_User__c=userZM.Id
        ,   AOP_Regional_Vol_in_kL__c = aop.AOP_Regional_Vol_in_kL__c
        ,   AOP_New_Win_in_kL__c = aop.AOP_New_Win_in_kL__c
        ,   AOP_DSO__c = aop.AOP_DSO__c
        ,   AOP_Forecast_Accuracy__c = aop.AOP_Forecast_Accuracy__c
        ,   AOP_National_Vol_in_kL__c = aop.AOP_National_Vol_in_kL__c
        ,   AOP_Regional_GP__c = aop.AOP_Regional_GP__c
        ,   AOP_National_GP__c = aop.AOP_National_GP__c
        ,   AOP_Focus_Vol_in_kL__c = aop.AOP_Focus_Vol_in_kL__c
        ,   Month__c = 'Oct'
        );

        Monthly_line_Logs__c mon1 = new Monthly_line_Logs__c
        (   Monthly_line__c = opp.OwnerId
        ,   Monthly_Opportunity__c = opp.Id
        ,   MTD_Regional_Vol_in_kL__c = opp.MTD_Volume__c
        ,   MTD_Focus_Vol_in_kL__c = opp.MTD_Focussed_Volume__c
        ,   MTD_Regional_GP__c = opp.MTD_GP__c
        ,   MTD_New_Win_in_kL__c = opp.MTD_Volume__c
        );

        test.startTest();
        try
        {
            insert aopLine3;
            insert mon1;
        }
        catch ( Exception exceptionThrown )
        {
            // eat the exception...
        }
      
        opp.StageName = 'Closed';
        update opp;
        test.stopTest();
    }
}
</pre>