0

I am working on one lightning component and wrote an apex class as a controller. My class is below.

public class CampaignMemberController {
    @AuraEnabled
    public static List<CampaignMember> getCampaignMember(String Keys) {
        String keyword = Keys + '%' ;
        return [Select Id,Name,Owner__c,CompanyOrAccount,LastName,Last_Activity_Date__c,Campaign_Name__c, Status, Phone, Email From CampaignMember WHERE Campaign_Name__c LIKE:keyword AND Campaign.IsActive=true LIMIT 50];
    }
}

Can anyone help me out in writing test class for the same. I tried below code but its not showing in code coverage.

    @isTest(SeeAllData=true)
       public class CampaignMemberControllertest{
@AuraEnabled
public static testmethod void CampaignMemberControllertest()
{
       Test.startTest();
        Account acct = new Account (Name = 'Acme, Inc.');
         insert acct;
       system.debug('Inserted Account, ID: ' + acct.id);

Contact con = new Contact(
                  FirstName = 'Robin',
                  LastName = 'Koehler',
                  AccountId = acct.Id
                  );
insert con;   


Contact con1 = new Contact(
                  FirstName = 'Robin',
                  LastName = 'Koehler',
                  AccountId = acct.Id
                  );
insert con1;  

Campaign camp = new Campaign(
                    Name = 'Test',
                    IsActive = TRUE
                    );            
insert camp;


 Campaign camp1 = new Campaign(
                    Name = 'Test',
                    IsActive = TRUE
                    );            
insert camp1;

CampaignMember member = new CampaignMember(
    ContactId = con.Id,
    Status = 'sent',
    CampaignId = camp.Id
    ); 
insert member; 


CampaignMember member1 = new CampaignMember(
    ContactId = con1.Id,
    Status = 'sent',
    CampaignId = camp1.Id
    ); 
}

}

Can anyone help me out in this issue please.

3

There are two changes that you need to make in order to cover your controller

  1. You are not assigning value to Campaign_Name__c in CampaignMember object, and as SOQL has a filter on Campaign_Name__c field, you won't get any results out of your SOQL query.
  2. You have not called your getCampaignMember method from the test class due to which your controller won't be covered.

Make these below changes in the code to cover your controller. Also, note that it is recommended to use List to insert/update multiple records at a time instead of having multiple insert statements.

List<CampaignMember> members = new List<CampaignMember>();
CampaignMember member = new CampaignMember(
    ContactId = con.Id,
    Status = 'sent',
    CampaignId = camp.Id,
    Campaign_Name__c = 'Test Member1'
    ); 
members.add(member); 

CampaignMember member1 = new CampaignMember(
    ContactId = con1.Id,
    Status = 'sent',
    CampaignId = camp1.Id,
    Campaign_Name__c = 'Test Member2'
    ); 

members.add(member2); 
insert members;

Test.startTest();
List<CampaignMember> campaignMembers = CampaignMemberController.getCampaignMember('Test');
System.assertEquals(2, campaignMembers.size());
Test.stopTest();
  • Hello Vinay Ganji, thanks a lot for the update, I did made changes and got 100% code coverage. Thank you somuch for helping me out. – Venkata Sainath Jan 23 at 9:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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