function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AnzarCRMAnzarCRM 

Custom Controller Test Class

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

try this I hope it will cover  your catch block, I set OwnerId to null so that it give exception

public static testMethod void myUnitTestCatchBlock() {

List<Campaign> campaigns = new List<Campaign>();
for (Integer i=0; i < 10; i++){
Campaign p = new Campaign();
p.Name = 'testName' + i;
p.IsActive = true;
p.Type = 'Télémarketing';
p.Status = 'In Progress';
p.Description = 'testDescription' + i;
campaigns.add(p);
}

try{
Test.starttest();
insert campaigns;
Test.setCurrentPageReference(Page.VFP01_SelectHermesActiveCampaign);
Campaign campObj = campaigns.get(0);
campObj.OwnerId = null;
ApexPages.StandardController sc = new ApexPages.StandardController(campObj);
VFC01_SelectHermesActiveCampaign cls = new VFC01_SelectHermesActiveCampaign(sc);
List<AcmecomComToolsadmin.CampaignOutbound> listActiveCampaigns = cls.listActiveCampaigns;
String selectedHermesCampaign = cls.selectedHermesCampaign;
cls.selectedHermesCampaign = 'Testcorrespondante';
cls.saveSelectedHermesCampaign();
} catch (DmlException e){
System.debug('We caught a DML exception: ' + e.getDmlMessage(0));
ApexPages.addMessages(e);
}
Test.stoptest();

}

 And this Code will never get covered 

listActiveCampaigns = new VocalcomComToolsadmin.ReadToolsSoap().ListActiveCampaign('1').CampaignOutbound

 

because web servoce call out are not covered in testMethod

 

Let me know if any issues

 

All Answers

Shashikant SharmaShashikant Sharma

try this, It should improve your code coverage

@isTest
private class VFC01_SelectHermesActiveCampaign_Test {

    public static testMethod void myUnitTest() {
        
        List<Campaign> campaigns = new List<Campaign>();
        for (Integer i=0; i < 10; i++){
            Campaign p = new Campaign();
            p.Name = 'testName' + i;
            p.IsActive = true;
            p.Type = 'Télémarketing';
            p.Status = 'In Progress';
            p.Description = 'testDescription' + i;
            campaigns.add(p);
        }
        
        
        try{
            Test.starttest();
        
            insert campaigns;
            Test.setCurrentPageReference(Page.VFP01_SelectHermesActiveCampaign);
            ApexPages.StandardController sc = new ApexPages.StandardController(campaigns.get(0));
            VFC01_SelectHermesActiveCampaign cls = new VFC01_SelectHermesActiveCampaign(sc);
            List<VocalcomComToolsadmin.CampaignOutbound> listActiveCampaigns = cls.listActiveCampaigns;
            String selectedHermesCampaign = cls.selectedHermesCampaign;
            cls.selectedHermesCampaign = 'Testcorrespondante';
            cls.saveSelectedHermesCampaign();
            catch (DmlException e){
            System.debug('We caught a DML exception: ' + e.getDmlMessage(0));
            ApexPages.addMessages(e);    
           }
        Test.stoptest();
             }
}

 

Shashikant SharmaShashikant Sharma

Ok in that case let me tell you you can not make a webservice call out from a est method and test method will fail if you do so. You need to make a chaneg in your contrller

 

Change this

public List<VocalcomComToolsadmin.CampaignOutbound> listActiveCampaigns {
        get {
            if(listActiveCampaigns == null)
                listActiveCampaigns = new VocalcomComToolsadmin.ReadToolsSoap().ListActiveCampaign('1').CampaignOutbound;
            return listActiveCampaigns;
        }
        private set;
    }

 to

public List<VocalcomComToolsadmin.CampaignOutbound> listActiveCampaigns {
        get {
            if(listActiveCampaigns == null)
                if(!Test.isRunningTest())
{
listActiveCampaigns = new VocalcomComToolsadmin.ReadToolsSoap().ListActiveCampaign('1').CampaignOutbound;
}
else
{
   
   listActiveCampaigns = new List<VocalcomComToolsadmin.CampaignOutbound>();
//You can create a dummy response similar to what your webservice might have returned
}
            return listActiveCampaigns;
        }
        private set;
    }

 and try with test method from last post

AnzarCRMAnzarCRM

Now it's better, code coverage is 79%.

 

Could you please help to write a dummy response similar to what the webservice might have returned ? I have imported the WSDL, and the proxy created some classes that implements the SOAP message. The method that i'm using from the controller is "ListActiveCampaign" -->

listActiveCampaigns = new VocalcomComToolsadmin.ReadToolsSoap().ListActiveCampaign('1').CampaignOutbound;

 

Do i need to create a dummy response, or with 79% code coverage, it's sufficient ?

 

All the best,

Shashikant SharmaShashikant Sharma

Yes I can but I think you need that to increase coverage,the code that is not covered must be the catch block , to do so add this method as well

 

Try with this, add this one more test method , this should increase you code coverage by covering catch block  as well

 

    public static testMethod void myUnitTestCatchBlock() {
        
        List<Campaign> campaigns = new List<Campaign>();
        for (Integer i=0; i < 10; i++){
            Campaign p = new Campaign();
            p.Name = 'testName' + i;
            p.IsActive = true;
            p.Type = 'Télémarketing';
            p.Status = 'In Progress';
            p.Description = 'testDescription' + i;
            campaigns.add(p);
        }
        
        
        try{
            Test.starttest();
        
            insert campaigns;
            Test.setCurrentPageReference(Page.VFP01_SelectHermesActiveCampaign);
            ApexPages.StandardController sc = new 
Campaign campObj = campaigns.get(0);
campObj.OwnerId = 'InvalidId'; 
ApexPages.StandardController(campObj);
            VFC01_SelectHermesActiveCampaign cls = new VFC01_SelectHermesActiveCampaign(sc);
            List<VocalcomComToolsadmin.CampaignOutbound> listActiveCampaigns = cls.listActiveCampaigns;
            String selectedHermesCampaign = cls.selectedHermesCampaign;
            cls.selectedHermesCampaign = 'Testcorrespondante';
            cls.saveSelectedHermesCampaign();
            catch (DmlException e){
            System.debug('We caught a DML exception: ' + e.getDmlMessage(0));
            ApexPages.addMessages(e);    
           }
        Test.stoptest();
         
    }

 

Shashikant SharmaShashikant Sharma

try with this 

change this in your controller

catch(DMLException ex) {
            ApexPages.addMessages(ex);
            return null;

 to 

catch(Exception ex) {
            ApexPages.addMessages(ex);
            return null;

 So that it catches all kind of exception.

AnzarCRMAnzarCRM

No more error, however still covering 79%. It indicates that the catch is not covered.

Shashikant SharmaShashikant Sharma

try this I hope it will cover  your catch block, I set OwnerId to null so that it give exception

public static testMethod void myUnitTestCatchBlock() {

List<Campaign> campaigns = new List<Campaign>();
for (Integer i=0; i < 10; i++){
Campaign p = new Campaign();
p.Name = 'testName' + i;
p.IsActive = true;
p.Type = 'Télémarketing';
p.Status = 'In Progress';
p.Description = 'testDescription' + i;
campaigns.add(p);
}

try{
Test.starttest();
insert campaigns;
Test.setCurrentPageReference(Page.VFP01_SelectHermesActiveCampaign);
Campaign campObj = campaigns.get(0);
campObj.OwnerId = null;
ApexPages.StandardController sc = new ApexPages.StandardController(campObj);
VFC01_SelectHermesActiveCampaign cls = new VFC01_SelectHermesActiveCampaign(sc);
List<AcmecomComToolsadmin.CampaignOutbound> listActiveCampaigns = cls.listActiveCampaigns;
String selectedHermesCampaign = cls.selectedHermesCampaign;
cls.selectedHermesCampaign = 'Testcorrespondante';
cls.saveSelectedHermesCampaign();
} catch (DmlException e){
System.debug('We caught a DML exception: ' + e.getDmlMessage(0));
ApexPages.addMessages(e);
}
Test.stoptest();

}

 And this Code will never get covered 

listActiveCampaigns = new VocalcomComToolsadmin.ReadToolsSoap().ListActiveCampaign('1').CampaignOutbound

 

because web servoce call out are not covered in testMethod

 

Let me know if any issues

 

This was selected as the best answer
AnzarCRMAnzarCRM

95% code coverage.

 

A lot of thanks for help.

 

All the best.

Shashikant SharmaShashikant Sharma

Your welcome mate

AnzarCRMAnzarCRM

I have just created a new topic "APEX - CallCenter - Parse XML File ". In my controller, i would like to get the CustomerId from the CallCenter configuration file. Currently, i pass this parameter as static value : 1. Could you, please help ?

 

All the best.