0

I write a test class to cover standard set controller class on account but i am not able to pass id. thats why not able cover code coverage.

here is my code

@IsTest
public class ClientReachEmailPageControllerTest {

    private static testMethod void test1() {
        User u = CreateOrder.createUser('testemail@emailmakerdetail.com', 'User Order Maker Details');
        insert u;

        // Create Client
        List<Account> acc= new List<Account>();
        Account client = new Account(LastName='TestAccount', personemail = 'test@test.com',Asst_Email__pc = 'test123@gmail.com');
        acc.add(client);
        Account client1 = new Account(LastName='TestAccount1', personemail = 'test@test.com',Asst_Email__pc = 'test123@gmail.com');
        acc.add(client1);

        //insert client;
        insert acc;

        // Create Order
        Order__c hOrder = CreateOrder.createHOrderItem(u.Id, u.Id, acc[0].Id, 'Test  Details');
        hOrder.Coat_Maker1__c ='a066YT000005c2vT';
        insert hOrder;        

        // Create Controller
        Test.startTest();       
        Test.setCurrentPage(Page.ClientReachEmailPage);
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acc);
        ClientReachEmailPageController obj = new ClientReachEmailPageController(stdSetController);  

        PageReference pageRef = Page.ClientReachEmailPage;        
        Test.setCurrentPage(pageRef);
        Test.stopTest();
    }

}
0

For StandardSetController, you don't have to pass the ID, instead you will have to pass the list of records. You can set the records for StandardSetController using setSelected method. Below test class should cover your controller.

@isTest
public class ClientReachEmailPageControllerTest {
    private static testMethod void test1() {
        User u = CreateOrder.createUser('testemail@emailmakerdetail.com', 'User Order Maker Details');
        insert u;

        // Create Client
        List<Account> acc= new List<Account>();
        Account client = new Account(LastName='TestAccount', personemail = 'test@test.com',Asst_Email__pc = 'test123@gmail.com');
        acc.add(client);
        Account client1 = new Account(LastName='TestAccount1', personemail = 'test@test.com',Asst_Email__pc = 'test123@gmail.com');
        acc.add(client1);

        //insert client;
        insert acc;

        // Create Order
        Order__c hOrder = CreateOrder.createHOrderItem(u.Id, u.Id, acc[0].Id, 'Test  Details');
        hOrder.Coat_Maker1__c ='a066YT000005c2vT'; //If this is a relationship field, create a record for Coat Maker and assign the Id of that record to this field, if its a text field, it will work else the test class will fail.
        insert hOrder;        

        // Create Controller
        Test.startTest();       
        Test.setCurrentPage(Page.ClientReachEmailPage);
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acc);
        stdSetController.setSelected(acc);
        ClientReachEmailPageController obj = new ClientReachEmailPageController(stdSetController);  
        Test.stopTest();
    }
}

Note : You should always add assert statements in your test class to check if your code is working as expected.

  • Thanks Vijay for reply its working fine for me. – Hennry Jan 31 at 9:07
  • That hard-coded Salesforce Id looks like a problem waiting to happen. Is Coat_Maker1__c a Text field? I don't see how the unit test passes otherwise. – David Reed Jan 31 at 12:44
  • Yup, you are right @DavidReed, that test class would fail unless it is a text field. Added a comment. – Vijay Ganji Jan 31 at 12:55

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.