+ Start a Discussion
Vijay sidaraddiVijay sidaraddi 

test class not covering the code coverage

Hi Friends,

Can some one help me where im wrong in this class and test class ,which is not covering minimum code coverage of 75% .also i have made bold things in test class which i couldnt able to satisfy in If statement can someone help on this.

Class Name :JenkinsConnectEmailServiceHandler 
global class JenkinsConnectEmailServiceHandler implements Messaging.InboundEmailHandler{
    
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
        String subject = email.subject;
        List<String> subjectSplitList = subject.split('»'); 
        System.debug('*** subjectSplitList *** '+subjectSplitList);
        String stageDetails = subjectSplitList[subjectSplitList.size()-1];
        System.debug('*** Stage Name *** '+stageDetails);
        
        List<String> stageDetailsList = stageDetails.split('-');
        String stageName = stageDetailsList[0].trim();
        String buildNumberString = stageDetailsList[1].trim();
        String stageStatus = stageDetailsList[2].trim();
        Deployment_Stage__c stageObj;
        Decimal buildNumber = 0;
        for(String s: buildNumberString.split('#')){
            if(s.trim().isNumeric()){
                buildNumber = Decimal.valueOf(s.trim());
            }
        }
        
        //Map<String, String> stagesMap = new Map<String, String>();
        String stageLabel = '';
        for(Stages__c st: Stages__c.getAll().values()){
            //stagesMap.put(st.Stage_Name__c, st.Name);
            if(String.isEmpty(stageLabel) && stageName.containsIgnoreCase(st.Name)){
                stageLabel = st.Stage_Name__c; 
            }
        }
        
        /*for(Deployment__c dep: [select Id, (Select Id, Name, Build_Number__c, Stage_Label__c from Deployment_Stages__r 
                                            where  Stage_Label__c =: stageLabel AND Build_Number__c =: buildNumber)
                                from Deployment__c order by LastModifiedDate desc limit 1]){
          */                      
            for(Deployment_Stage__c stage: [Select Id, Name, Build_Number__c, Stage_Label__c from Deployment_Stage__c 
                                            where  Stage_Label__c =: stageLabel AND Build_Number__c =: buildNumber]){
                System.debug('*** stage *** '+stage);
                stageObj = stage;
                break;
                /*if(stageName.containsIgnoreCase(stagesMap.get(stage.Stage_Label__c)) && (buildNumber == stage.Build_Number__c)){
                    System.debug('*** stage matched *** '+stage.Stage_Label__c);
                    
                }*/
            }
        //}
        if(stageObj != null){
            List<Attachment> attachmentList = new List<Attachment>();
            // get attachments, if any
            if(email.textAttachments != null){
                for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
                    Attachment attachment = new Attachment();
                    attachment.Name = tAttachment.fileName;
                    attachment.Body = Blob.valueOf(tAttachment.body);
                    attachment.ParentId = stageObj.Id;
                    attachmentList.add(attachment);
                }
            }
            if(email.binaryAttachments != null){
                for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
                  Attachment attachment = new Attachment();
                 
                  attachment.Name = bAttachment.fileName;
                  attachment.Body = bAttachment.body;
                  attachment.ParentId = stageObj.Id;
                  attachmentList.add(attachment);
                }
            }
            insert attachmentList;
        }
        return null;
    }
}

****************************below is the test class for above class**********

@isTest
public class JenkinsConnectEmailServiceHandlerTests {
    public static testMethod void TestJenkinsConnectEmailServiceHandler()
    {
        RM_Configuration__c RConfig = new RM_Configuration__c();
        RConfig.RecordTypeid = DeploymentControllerService.getRecordTypeIdByName('RM_Configuration__c', 'RM Config');
        insert RConfig;
        Release__c re = new Release__c();
        re.Name='Release90';
        re.Status__c = 'Yellow';
        insert re;
        Deployment__c attachmenttext = new Deployment__c();    
        attachmenttext.Name = 'Build # 67';
        attachmenttext.Release__c = re.id;
        
        insert attachmenttext;
        Deployment_Stage__c DStage = new Deployment_Stage__c();
        DStage.Name = 'DevOps_CR_novasuite_code_analysis';
        DStage.Deployment__c = attachmenttext.id;
        Insert DStage;
        
        //attachmenttext.stage.Name = 'testing';
        // create a new email and envelope object
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();  
        
        // setup the data for the email
        email.subject = 'Devops_Internal » Internal_Team » DevOps_CR_novasuite_code_analysis - Build # 67 - Successful: Check console output at http://52.49.193.65/jenkins/job/Devops_Internal/job/Internal_Team/job/DevOps_CR_novasuite_code_analysis/67/'; 
        email.fromAddress = 'someaddress@email.com';
        email.plainTextBody = 'email body\n2225256325\nTitle';
        
        // add an Binary attachment
        Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
        attachment.body = blob.valueOf('my attachment text');
        attachment.fileName = 'textfileone.txt';
        attachment.mimeTypeSubType = 'text/plain';
        email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
            
            // add an Text atatchment
            Messaging.InboundEmail.TextAttachment attachmentname = new Messaging.InboundEmail.TextAttachment();
        attachmentname.body = 'my attachment text';
        attachmentname.fileName = 'textfiletwo3.txt';
        attachmentname.mimeTypeSubType = 'texttwo/plain';
        email.textAttachments =   new Messaging.inboundEmail.TextAttachment[] { attachmentname };
            
            JenkinsConnectEmailServiceHandler testInbound=new JenkinsConnectEmailServiceHandler ();
        testInbound.handleInboundEmail(email, env); 
        
         
        
            Stages__c st1=new Stages__c();
        st1.Name='DevOps_CR_novasuite_code_analysis';
        
             insert st1;
        
        
        String subject = email.subject;
        List<String> subjectSplitList = subject.split('»');
        String stageDetails = subjectSplitList[subjectSplitList.size()-1];
        List<String> stageDetailsList = stageDetails.split('-');
        String stageName = stageDetailsList[0].trim();
        String buildNumberString = stageDetailsList[1].trim();
        String stageStatus = stageDetailsList[2].trim();
        Deployment_Stage__c stageObj;
        Decimal buildNumber = 0;
        for(String s: buildNumberString.split('#')){
            if(s.trim().isNumeric()){
                buildNumber = Decimal.valueOf(s.trim());
            }
        }
        String stageLabel = '';
        for(Stages__c st: Stages__c.getAll().values()){
            //stagesMap.put(st.Stage_Name__c, st.Name);
            if(String.isEmpty(stageLabel) && stageName.containsIgnoreCase(st.Name)){
                stageLabel = st.Stage_Name__c; 

            }
        }
       
    }
}

Thanks
Vijaykumar S
Eric PepinEric Pepin
Custom Settings do not exist in a test context. In your code, you do: Stages__c.getAll().values() and it won't return anything because your Custom Settings don't exist.