+ Start a Discussion
mk2013mk2013 

Test Coverage help please

I am a newbie to Salesforce and trying to get maximum code coverage in my test class.

I have a delete method in my controller class which deletes a custome object from external application vis a webservice call and if that is successful then it deletes it from SalesForce db.

My controller code is like below.

 

 

WebService static String deleteQuote(String quoteId) {
          HttpRequest req = new HttpRequest(); 
          String retString = 'false';
          req.setMethod('GET');
          req.setHeader('Connection','keep-alive');
          req.setEndpoint('http://aaaa.bbbb.com/MyWebApp/deleteQuote.htm?id=' +quoteId);

          Http http = new Http();
          HTTPResponse res = http.send(req);  
          if(res.getBody().equalsIgnoreCase('true')){
              retString = deleteQuoteFromSFDB(quoteId);
          }
         return retString;
    }
    
    public static String deleteQuoteFromSFDB(String quoteId){
      String returnStr =  'false';
      List<Custom_Quote__c> quotes = new List<Custom_Quote__c>();
        quotes = [SELECT Id, Name FROM Custom_Quote__c q where q.name = : quoteId];
	try {
            delete quotes;
            returnStr =  'true';
        } catch (DmlException e) {
            for (Integer i = 0; i < e.getNumDml(); i++){
                System.debug(e.getDmlMessage(i)); 
            }
        }
        return returnStr;
    }
    

 

In my test class I have following code.

      /*****************DML Start*********************/
           Account testAccount = new Account();
           testAccount.Name = 'test_99';
           testAccount.Protected_Account__c = 'yes';
           insert testAccount;
                 
           Opportunity o = new opportunity();
           o.Name = 'OppTest';
           o.AccountId = testAccount.Id;
           o.CloseDate = Date.today();
           o.StageName = '1. Prospecting';
           o.customer_type__c = 'zba';
      
           insert o;
           System.assertEquals(testAccount.Name, 'test_99');
           System.assertEquals(o.Name, 'OppTest');
           
           Custom_Quote__c eq = new Custom_Quote__c();
           eq.Name = '123456';
           eq.Package_Type__c = 'Lease';

           eq.Quote_Summary__c = 'Summary';
           eq.Quote_Total__c = 12334.21;
           eq.Opportunity__c = o.Id;
           insert eq; 
        /*****************DML complete.*********************/





         /********** Test Delete Quote Happy path**********************/
         String delRet = MyController.deleteQuoteFromSFDB(eq.Name);
         System.debug('\n\n\n delet ret = ' + delRet);
         System.assertEquals('true', delRet);
         
         /********** Test Delete Quote Exception path**********************/
         Custom_Quote__c eq1 = new Custom_Quote__c();
         eq1.Name = '123456';
         eq1.Package_Type__c = 'Lease';
         eq1.Quote_Summary__c = 'Summary';
         eq1.Quote_Total__c = 12334.21;
         eq1.Opportunity__c = o.Id;
         insert eq1;
        
	 delRet = MyController.deleteQuoteFromSFDB('xyzabd');
         System.debug('\n\n\n delete ret = ' + delRet);
         System.assertEquals('true', delRet);

 I thought passing a non-existing quote name to the deletefromSFDB method will create a DMLException situation and I can get code coverage for exception block, but I am not getting any exception there and the methid returns true.

 

How do I get coverage for the DMXException block? Please help.

Thanks,

mk2013