+ Start a Discussion

Test Coverage for StandardSetController Class .How i can coverage it in my code

Hi We have an tricky situation my all the apex class are covered more then 85% except one class use an StandardSetController Class  .Here is my sample code how i can cover the code. Somebody can help me to cover the line in test coverage.

List<Custom Object> variable1 = (List<Custom Object>)xyz.getSelected(); I am getting the p_stdSetController from my constructor here is the code ApexPages.StandardSetController xyz. List<Custom Object> variable1 = (List<Custom Object>)xyz.getSelected(); this line is hitting the code coverage .




I think what you are looking for is setRecords(); on the standardsetcontroller injected into the extension.


Something like this:



public class extension { public ApexPages.StandardSetController con; Boolean success; public Extension(ApexPages.standardSetController con) { this.con = con; success = false; } public void save() { if(con.getSelected().size() > 0) { success = true; } } public static testmethod void testselected() { List<Account> accounts = new List<Account>(); for(Integer i=0;i<10;i++) { accounts.add(new Account(Name = 'TEST' + i)); } insert accounts; ApexPages.StandardSetController con = new ApexPages.StandardSetController(accounts); con.setSelected(accounts); Extension ext = new Extension(con); Test.startTest(); ext.save(); Test.stopTest(); System.assert(ext.success); } }


 Does that help?


It is not sufficent to us,i will provide sample code
Above method does not work in test class, it still throws the exception like "Collection contains modified rows" , I tried searching over but could not understand the reason but below fix works :- ApexPages.StandardSetController con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id,Name FROM Account])); Directly instantiating the standard set controller by query locator works fine.