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
ManidManid 

test class for standardsetcontroller

public class MassDelete {
    public list<account> accs {get;set;}
    public MassDelete(apexpages.StandardSetController cont){
        string[] fields=new string[]{'name','industry','rating','phone'};
            cont.addfields(fields);
        accs=new list<account>();
        accs=(list<account>)cont.getselected();
    }
    public pagereference deletes(){
        delete accs;
        pagereference p=new pagereference('/search/UndeletePage');
        return p;
    }
}
my test class:
@isTest
public class MassDeleteTest {
    
    testmethod static void testme(){
        list<account> accs=new list<account>();
        account a=new account(name='Test name');
        insert a;
        account a2=new account(name='Acme pro');
        insert a2;
        accs.add(a);
        accs.add(a2);
        test.startTest();
        pagereference pref=page.massDeleteAcc;
        test.setCurrentPage(pref);
        apexpages.standardsetcontroller std=new apexpages.StandardSetController(accs);
        std.setselected(accs);
        MassDelete obj=new Massdelete(std);
        test.stopTest();
        
    }
}
i didn't get full coverage why?
 
Deepak Pandey 13Deepak Pandey 13
You are not covering pgRef method-
@isTest
public class MassDeleteTest {
    testmethod static void testme(){
        list<account> accs=new list<account>();
        account a=new account(name='Test name');
        insert a;
        account a2=new account(name='Acme pro');
        insert a2;
        accs.add(a);
        accs.add(a2);
        test.startTest();
        pagereference pref=page.massDeleteAcc;
        test.setCurrentPage(pref);
        apexpages.standardsetcontroller std=new apexpages.StandardSetController(accs);
        std.setselected(accs);
        MassDelete obj=new Massdelete(std);
        obj.deletes();
        test.stopTest();
    }
}
ManidManid
same output 30% coverage i try everything i am not getting proper output thats why i post my code in the community
Amit Chaudhary 8Amit Chaudhary 8

Please check below post for sample test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Please try below code
@isTest
public class MassDeleteTest {
    testmethod static void testme()
	{
		List <Account> lstAccount = new List<Account>();
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		lstAccount.add(testAccount);

		Account testAccount1 = new Account();
		testAccount1.Name='Test Account1' ;
		lstAccount.add(testAccount1);

		insert  lstAccount;
 
        test.startTest();
		
			Test.setCurrentPage(Page.massDeleteAcc);
			apexpages.standardsetcontroller std=new apexpages.StandardSetController(lstAccount);
			std.setselected(lstAccount);
			
			MassDelete obj=new MassDelete(std);
			obj.deletes();
			
		test.stopTest();
    }
}

Please excute the above code and let us know if you will get any error and you will not get 100% code coverage please post which lines are not covred

 
Amit Chaudhary 8Amit Chaudhary 8
I tested your code in my org and found you are getting "You cannot call addFields when the data is being passed into the controller by the caller" in test class due to below code in apex class
string[] fields=new string[]{'name','industry','rating','phone'};
 cont.addfields(fields);

Try to updated your code like below
public class MassDelete {
    public list<account> accs {get;set;}
    public MassDelete(apexpages.StandardSetController cont){
        string[] fields=new string[]{'name','industry','rating','phone'};
        if (!Test.isRunningTest()) cont.addFields(fields);
        accs=new list<account>();
        accs=(list<account>)cont.getselected();
    }
    public pagereference deletes(){
        delete accs;
        pagereference p=new pagereference('/search/UndeletePage');
        return p;
    }
}
Test class should be like below
@isTest
public class MassDeleteTest {
    testmethod static void testme()
	{
		List <Account> lstAccount = new List<Account>();
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		lstAccount.add(testAccount);

		Account testAccount1 = new Account();
		testAccount1.Name='Test Account1' ;
		lstAccount.add(testAccount1);

		insert  lstAccount;
 
        test.startTest();
		
			Test.setCurrentPage(Page.massDeleteAcc);
			apexpages.standardsetcontroller std=new apexpages.StandardSetController(lstAccount);
			std.setselected(lstAccount);
			
			MassDelete obj=new MassDelete(std);
			obj.deletes();
			
		test.stopTest();
    }
}

Let us know if this will help you