+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

Test class Not cover when access Recode from custom setting

//Controller Class
Please Any one Help
Pardot_Credential_Setting__c is Custom Setting
public class pardotActivityController{

        public static Pardot_Credential_Setting__c customSetting(){
            Pardot_Credential_Setting__c objSetting = Pardot_Credential_Setting__c.getValues('Pardot Access');
            if(objSetting != null && objSetting.Sync_disabled__c == false){
                return objSetting;
            }
            return null;
        }
        public static String authMethod(){
            
            Pardot_Credential_Setting__c objSetting = customSetting();

            if(objSetting != null){    <----Not Covering -------->
                String email = objSetting.Pardot_Email__c;
                String password = objSetting.Pardot_Password__c;
                String userKey = objSetting.User_Api_Key__c;

                HttpRequest req = new HttpRequest();
                req.setEndpoint( objSetting.Pardot_BaseUrl__c+'/api/login/version/4' );
                req.setMethod( 'POST' );
                req.setBody( 'email=' + email + '&password=' + password + '&user_key=' + userKey );

                HttpResponse res = new Http().send( req );

                System.debug('>>>>>>>>res'+ res );
                System.debug('>>>>>>>>>res.getStatus()'+ res.getStatus() );
                System.debug('>>>>>>res.getBody()'+ res.getBody() );

                String response = res.getBody();
                
                Integer startIdx;
                Integer endIdx;
                String apiKey;
                if(response.contains('<api_key>')){
                    startIdx = response.indexOf( '<api_key>' ) + 9;
                    endIdx = response.indexOf( '</api_key>' );
                    apiKey = response.substring( startIdx, endIdx );
                    System.debug( '>>>>>>>apiKey'+ apiKey );
                    return apiKey;
                }
            }
            
            return null;
        }
}

<------Test class------->
 static Testmethod void authMethodTest(){ 
        Pardot_Credential_Setting__c objSetting = New Pardot_Credential_Setting__c();
         objSetting.Name = 'Test';
         objSetting.Pardot_Email__c = 'abc@gmail.com';
         objSetting.User_Api_Key__c = '<api_key>';
         objSetting.Pardot_BaseUrl__c = 'https://pi.pardot.com/api/login/version/4';
         objSetting.Pardot_Password__c = 'test12';
         objSetting.Sync_disabled__c = false;
         insert objsetting;
        	Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        	test.startTest();
        		pardotActivityController.authMethod();
        	test.stopTest();
    	}
 public class MockHttpResponseGenerator implements HttpCalloutMock{ //Http callout class for authmothod
            public HTTPResponse respond(HTTPRequest req){               
            String json=        '{'+
            '        "EMail": "leadsscotia@culliganwaterco.com",'+
            '        "Password": "<api_key>,'+
            '        "Userkey": "</api_key>",'+
            '}';
                   
            	System.assertEquals('https://pi.pardot.com/api/login/version/4', req.getEndpoint());
            	System.assertEquals('POST', req.getMethod());
                
                //make a fake responce
                 HttpResponse res = new HttpResponse();
                 res.setBody(json);
                 res.setStatusCode(200);
                 return res;
            }
        }

 
Best Answer chosen by Bablu Kumar Pandit
Agustin BAgustin B
Hi Bablu, yes I saw that line thats why I asked about the line 7. Where are you setting the value Pardot Access?Try replacing 'Pardot Access' with 'Test' as it is the name you set on the test method.

If you solve this issue with that fix then please mark as correct. If it is not working try using Pardot_Credential_Setting__c objSetting = Pardot_Credential_Setting__c.getall().values(); and you will pass line 17.

Please also mark my answer on your other question as correct, it is a reward for the time helping others.
Good luck

All Answers

Agustin BAgustin B
Hi Bablu, please close the other question marking my correct answer about creating the custom setting as i see you used that approach.

Looking this where are you setting a value Pardot Access? Maybe thats because you are not retrieving anything.
Bablu Kumar PanditBablu Kumar Pandit
Hii Agustin,
see the code Line no. 17 not covering after line number 17
so please guide me where i am mistake,How i correct
 
Agustin BAgustin B
Hi Bablu, yes I saw that line thats why I asked about the line 7. Where are you setting the value Pardot Access?Try replacing 'Pardot Access' with 'Test' as it is the name you set on the test method.

If you solve this issue with that fix then please mark as correct. If it is not working try using Pardot_Credential_Setting__c objSetting = Pardot_Credential_Setting__c.getall().values(); and you will pass line 17.

Please also mark my answer on your other question as correct, it is a reward for the time helping others.
Good luck
This was selected as the best answer
Bablu Kumar PanditBablu Kumar Pandit
Thanks Agustin It Worked
Agustin BAgustin B

Great bablu, if you can please mark as correct my comment on your previous question so it can be closed and help any other having the same inquiry.

Good luck