0

I need help to get more code coverage for the test class but I'm not sure how to approach it

Class:

 public  class salesforcetoJiraHandler  {

   public static void newmed ( List<Internal_Request__c> triggerNew, Map<ID,Internal_Request__c> mapNew, Map<ID,Internal_Request__c> mapOld){
    
        Jira_credential__mdt g = Jira_credential__mdt.getInstance('Admin');
        String obname = g.Fieldset1__c; 
        String FS = g.Fieldset2__c;
    
        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); 
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(obname);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(FS);
    
    
        list<String> fieldSetAPI = new list<string>();
        map<string,string> apiVslabelMap = new map<string,string>();
        for(Schema.FieldSetMember fieldSetMemberObj : fieldSetObj.getFields())
        {
            fieldSetAPI.add(fieldSetMemberObj.getFieldPath());
            apiVslabelMap.put(fieldSetMemberObj.getFieldPath(),fieldSetMemberObj.getLabel());
            system.debug('API Name ====>' + fieldSetMemberObj.getFieldPath()); 
        }
    
            Boolean its = false;
            for(ID Mapid: mapNew.keyset()){
    
        for(Schema.FieldSetMember fieldSetMemberObj : fieldSetObj.getFields()) {
    
            if (string.valueOF(mapOld.get(Mapid).get(fieldSetMemberObj.getFieldPath())) != string.valueOF(mapNew.get(Mapid).get(fieldSetMemberObj.getFieldPath())))  
            {
                its = true;
                
            }
            } 
        }
    
        if(its == true){
            checkStatus(triggerNew);
        }
    
       }
    
        
            public static void checkStatus (List<Internal_Request__c> IRT){
                List<ID> InRIds = new List <ID>();
                for(Internal_Request__c ir : (IRT)){
                    InRIds.add(ir.Id);
        
                }
                changeStatus(InRIds); 
                system.debug('What the trigger is sending: '+InRIds); 
                
               }
        
          
           @future(callout = true)
           public static void changeStatus(List<Id> irsID){ 
    
            List<Internal_Request__c> workR = [SELECT CreatedById, ID, key__c, Status_IR__c FROM Internal_Request__c WHERE ID IN :irsID];
                system.debug('What are we getting: ' +workR[0].Status_IR__c);
    
                
            String Status =  workR[0].Status_IR__c;
            String id =  workR[0].ID;
            String Cby = workR[0].CreatedById;
            
            system.debug(Status); 
            system.debug(id); 
           
                
            Map<String, Stat_Salesforce_to_Jira__mdt> ics = Stat_Salesforce_to_Jira__mdt.getAll();
            Map<String,Decimal> str = new Map <String, Decimal>();
            for (Stat_Salesforce_to_Jira__mdt vr : ics.values()) {
                  str.put(vr.Salesforce_Status__c, vr.Jira_Status_Id__c);
            }
          
                if(str.containskey(Status)){
                   
                 
    
                    Jira_credential__mdt globalmc = Jira_credential__mdt.getInstance('Admin');
        
                    string username = globalmc.Username__c;
                    string password = globalmc.Password__c; // is the security token from Atlassian account profile --> Manage your Account-->Security-->API token-->Create and manage API tokens--> Manage your account-->API tokens
                    
                    String url1 = globalmc.SalesforcetoJira_Url__c;
                    String url2 = globalmc.SalesforcetoJira_Url2__c;
                    
                    
                    
                    String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));
        
                    String url = url1+workR[0].key__c+url2;
        
                        Http http = new Http();
                        HttpRequest req = new HttpRequest();
                        req.setHeader('Content-Type', 'application/json;charset=utf-8');
                        req.setHeader('Accept','application/json');
                        req.setHeader('Authorization', auth_header);
                        req.setHeader('X-Atlassian-Token', 'nocheck');
                        req.setMethod('POST');
                        req.setEndpoint(url);
                        req.setBody('{ "transition": { "id": "'+str.get(Status)+'"}}');
    
                        try{
                            HttpResponse res = http.send(req);
                            system.debug(res);
                            
                        }
                        catch (Exception e) {
                            system.debug('Error: ' + e);
                        }
                
            }
          
            
           }         
    }




                               
  • TEST Classes

HttpCalloutMock

 @isTest
global class JiraInsertMockHttpResponseGenerator implements HttpCalloutMock  {
   global HTTPResponse respond(HTTPRequest req) {
        // Create a fake response 
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'application/json');
    res.setBody('{ "fields": { "project": { "key": "test" }, "summary": "test", "issuetype": { "name": "test" },  "description": "test" } }');
    
    res.setStatusCode(200);
    return res;
    }
}

Test for the class

@isTest
public class salesforcetoJiratest {
    public static testmethod void testsalesfortojiraHandler(){
     List<Internal_Request__c> testC = new List<Internal_Request__c>{new Internal_Request__C(Concerned_App__c ='Business test',     Type__c = 'test', 
                                                                                                Priority__c = 'Medium', Subject__c = 'test', Description__c = 'test',
                                                                                              Project_Name__c = 'test', Jira_Type__c = 'bug', URL_Jira__c = 'www.test.com',
                                                                                              Key__c = 'test',  Status_IR__c = 'Acknowledge', Jira_Key__c = 'test', 
                                                                                                Jira_Status__c = 'In Development' )};
                                                                                                    
       insert testC;
       
        
        
        HttpResponse res = new HttpResponse();
        res.setBody('{ "fields": { "project": { "key": "test" }, "summary": "test", "issuetype": { "name": "test" },  "description": "test" } }');
        res.setStatusCode(200);
        
        test.setMock(HttpCalloutMock.class, new JiraInsertMockHttpResponseGenerator());
        
        
        Test.startTest();
        Internal_Request__c tstITR = new Internal_Request__c();
        salesforcetoJiraHandler.checkStatus(testC);
        Test.stopTest();   
    }
}

For the moment I'm getting 34% of coverage but I'm not sure how to do it, any help or advice to get more code coverage?:

enter image description here

enter image description here

1
  • First thing, add a unit test for every public method of your class. Commented Sep 1, 2022 at 5:15

1 Answer 1

0

Based on the coverage report, it looks like the "newmed" method is being run - where is that method being called from and with what trigger context? Is your test method completing it's run or failing? Some more detail will assist.

EDIT: Validation of allowed values for a picklist still occurs when inserting data from a test class, so you'll need to update that first. Then, as the first comment on your post mentions, you should write a unit test for each public method in your class. To get coverage for the trigger portion of the class, write a test that updates an Internal_Request__c record that meets the logic for calling your code.

10
  • the newmed method is being used after an update from after update trigger trigger trigger_Jira on Internal_Request__c (after update) { if (trigger.isUpdate && !System.isFuture() && !System.isBatch()) { jira_Insert.issueCU(Trigger.New); salesforcetoJiraHandler.newmed(Trigger.new, trigger.newMap , trigger.oldMap); } } Commented Sep 1, 2022 at 14:26
  • it run but display that is failing Commented Sep 1, 2022 at 14:27
  • Can you paste the failure message?
    – BenBates
    Commented Sep 1, 2022 at 17:11
  • System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Status IR: bad value for restricted picklist field: Acknowledge: [Status_IR__c] Commented Sep 1, 2022 at 21:40
  • That's a pretty straight forward error - is 'Acknowledge' an allowed value for the "Status_IR__c" picklist field?
    – BenBates
    Commented Sep 1, 2022 at 22:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .