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
BellaBella 

Testing a New Class

Hi, I'm fairly new to Apex and I'm a little confused how the testing works. I have the following really small class but can't seem to figure out how to write the test to cover 75%. Can anyone help?

 

 

public class findSLCFamily { private final List<SLC__c> slcs; private final SLC__c currentSLC; public findSLCFamily() { currentSLC = [select Original_SLC__c from SLC__c where id = :ApexPages.currentPage().getParameters().get('id')]; slcs = [select Name, License_Order__c, Product__c, Devices__c, SUP_End_Date__c, Original_SLC__c from SLC__c where Original_SLC__c = :currentSLC.Original_SLC__c order by License_Order__c]; } public List<SLC__c> getSLCFamily() { return slcs; } }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kirrankirran

public class findSLCFamily { private final List<SLC__c> slcs; private final SLC__c currentSLC; public findSLCFamily() { currentSLC = [select Original_SLC__c from SLC__c where id = :ApexPages.currentPage().getParameters().get('id')]; slcs = [select Name,Original_SLC__c from SLC__c where Original_SLC__c = :currentSLC.Original_SLC__c ]; } public List<SLC__c> getSLCFamily() { return slcs; } public static testMethod void findSLCFamily_Test() { List<SLC__c> divList = new List<SLC__c>(); for(Integer i=0;i<15;i++) { SLC__c div = new SLC__c(Name='ram',Original_SLC__c='radhika'); divList.add(div); } insert divList; SLC__c sl = new SLC__C(Name='Test SLC',Original_SLC__c='radhika'); insert sl; ApexPages.currentPage().getParameters().put('id',sl.Id); findSLCFamily f=new findSLCFamily(); f.getSLCFamily(); } }

I got 100% coverage for the above code try it.

 

For any other information feel free to contact us.

DSKVAP developer's Team

Dskvap technologies.

email:kumar77@dskvap.com

All Answers

BellaBella
And just to clarify, this class is called when a button to display a visual force page is clicked. I'm not sure if that's relivant to the tests but I thought I'd put it out there. The controller component for it is called "SLCFamily".
kirrankirran

public class findSLCFamily { private final List<SLC__c> slcs; private final SLC__c currentSLC; public findSLCFamily() { currentSLC = [select Original_SLC__c from SLC__c where id = :ApexPages.currentPage().getParameters().get('id')]; slcs = [select Name,Original_SLC__c from SLC__c where Original_SLC__c = :currentSLC.Original_SLC__c ]; } public List<SLC__c> getSLCFamily() { return slcs; } public static testMethod void findSLCFamily_Test() { List<SLC__c> divList = new List<SLC__c>(); for(Integer i=0;i<15;i++) { SLC__c div = new SLC__c(Name='ram',Original_SLC__c='radhika'); divList.add(div); } insert divList; SLC__c sl = new SLC__C(Name='Test SLC',Original_SLC__c='radhika'); insert sl; ApexPages.currentPage().getParameters().put('id',sl.Id); findSLCFamily f=new findSLCFamily(); f.getSLCFamily(); } }

I got 100% coverage for the above code try it.

 

For any other information feel free to contact us.

DSKVAP developer's Team

Dskvap technologies.

email:kumar77@dskvap.com

This was selected as the best answer
BellaBella
Perfect! Thank you so much!