-2
public pagereference savePassword()
    {
    if((objAccountTemp.Contact_Name_1__c !=  null && objAccountTemp.Contact_Name_1__c !=  '' && objAccountTemp.Affiliate_User_Name__c != null && objAccountTemp.Affiliate_User_Name__c != '')){
        if(objAccountTemp.Affiliate_User_Name__c == objAccountTemp.Contact_Name_1__c){
            If(LoginType == 'affiliate') 
                {
                    String s = objAccountTemp.Contact_Name_1__c;
                    Account acc = new Account();
                    acc=[select Affiliate_Password__c from Account where id=:Affid limit 1];
                    acc.Affiliate_Password__c = s;
                    try{
                    update acc;
                    changePasswordMsg = true;
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Your password changed successfully!'));
                    IsEditMode = false;
                    IsSaveMode = true;
                    changePass = false; 
                    lstAcc[0].Affiliate_Password__c = s; 
                    changePassErrormsg = false;     
                    return null;
                    }catch(exception e){
                    L4LConstants.InsertErrorLog(e);
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Error Found!'));
                    return null;
                }
            }
        }
    }
}
  • 1
    try updating an Account record which doesn't exist. So it'll enter the catch block with DMLException – Poonam Apr 13 '15 at 6:06
  • Not sure why you have hardcoded zero for lstAcc. As a best practice, you would want to do a null and empty check on the list unless you are really sure that it would not be null and empty. We dont see how lstAcc is initialized in the code that you have shared. Try a scenario where lstAcc does not get any records and you code will definitely break and enter the catch block. – Rajiv Bhatt Apr 13 '15 at 6:20
  • Can we see the test code you've already written ? You haven't really explained why you have a hard time covering this in your test code, which sort of means you haven't really asked a question. – Samuel De Rycke Apr 13 '15 at 7:46
0

Please use below snippet in try block:

if(Test.isRunningTest())
  Integer i = 10/0;
0

Extract the code into a separate method and write tests for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.