-2

i have written a class i am trying get test coverage. i have written below. Could you please help me here

Class :

public class WrapperCam 

{

    public Boolean check{get;set;}
    public Campaign Cmp{get;set;}

    public WrapperCam(Id Idcc,String Nme)
    {
        this.check=false;
        this.Cmp = new Campaign(Id=Idcc,Name=Nme);
}
}

Test class:

@isTest

Public class WrapperCamtest
{

 @testSetup

  static void setupTestData(){

    test.startTest();

    Campaign campaign_Obj = new Campaign(id='00001',Name = 'suresh');

    Insert campaign_Obj; 

    test.stopTest();
  }
}

Let me know what should i add here so that i get 100% coverage

Thanks in advance!

0

2 Answers 2

0

If you are making a wrapper for the actual campaign object I would suggest the following wrapper:

public class campaignWrapper {

    public Boolean check{get;set;}
    public Campaign Cmp{get;set;}

    public campaignWrapper(Campaign cmp)
    {
        this.check=false;
        this.Cmp = cmp;
    }
}

as for the test class I like the way you start by making an actual campaign as this is a more "user test", so I continue from there:

@isTest
Public class campaignWrapperTest{

    // @testSetup is for providing a "start" for all your other test functions and will not add code coverage
    @testSetup
    static void setupTestData(){

        Campaign campaign_Obj = new Campaign(Name = 'suresh');
        Insert campaign_Obj; 

    }

    @isTest //the actual test function
    private static void testClass()
    {
        Campaign cmp = [select Id from Campaign where name = 'suresh'];
        test.startTest();
        campaignWrapper cmpWrapper = new campaignWrapper(cmp);
        test.stopTest();
        // do some checks (example)
        // System.assert(!cmpWrapper.check);
    }

}
0

To cover your constructor defined in the apex class, you need to create a constructor of the class in the test class too. See below:-

@isTest
Public class WrapperCamtest
{
    @isTest
    private static void testClass()
    {
        Id ids; String nam;
        WrapperCam controller = new WrapperCam(ids,nam);
    }
}

Note:- You do not have in test method in your test class. You don't need the data to cover your test class.

1
  • Thanks i added method and it worked
    – Ram
    Commented Nov 7, 2019 at 6:23

Not the answer you're looking for? Browse other questions tagged .