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
Kiran GKiran G 

Help Test Class for an extension

Hi Everyone,
I need help with a test class for the apex class below. I have got to somewhere with some help but I am still getiing errors with the code.

Extension Class:
public with sharing class ProcurementExtension {
private ApexPages.StandardController controller;
 
    private Set<String> ProcurementFields = new Set<String>();
     
    public ProcurementExtension (ApexPages.StandardController controller) {
        this.controller = controller;
        Map<String, Schema.SobjectField> fields =
        Schema.SobjectType.Procurement__c.fields.getMap();
 

        for (String s : fields.keySet()) {
        // Only include accessible fields
            if (fields.get(s).getDescribe().isAccessible() &&
                fields.get(s).getDescribe().isCustom()) {
                    ProcurementFields.add(s);
            }
        }
    }
     
    public  List<String> availableFields {
        get {
            controller.reset();
            controller.addFields(new List<String>(ProcurementFields));
                return new List<String>(ProcurementFields);
        }
    }
}

Test Class:

@isTest
private class testProcurementExtension{
    Private static testMethod void ProcurementExtension(){
        Account acc=new Account(Account_Type__c='Product Vendor'); {need to also add Account_Type__c='Service Provider' which is also a filter criteria}
        acc.Name='TestAccount';
        acc.Customer_Tin_CST__c='Test CST';
        insert acc;
        acc=[SELECT id,Name, Customer_Tin_CST__c FROM Account WHERE id=:acc.Id];
        System.assertEquals(acc.Name,'TestAccount');
        System.assertEquals(acc.Customer_Tin_CST__c,'Test CST');
        Procurement__c objProcurement = new Procurement__c();
        objProcurement.Vendor__c = acc.id;        

        insert objProcurement;
        objProcurement=[SELECT id,Name FROM Procurement__c WHERE id=:objProcurement.Id];
        System.assertEquals(objProcurement.Name,'Test');
        

        
        Test.startTest();
          ApexPages.StandardController stdCon=new ApexPages.StandardController(objProcurement);
          ProcurementExtension pex=new ProcurementExtension(stdCon);
        Test.stopTest();

    }
    }

Error

System.AssertException: Assertion Failed: Expected: JLS P - 121215 - 100001, Actual: Test

Please note:
Vendor_c is a lookup to accounts with crieteria that the Account_Type__c='Product Vendor' or 'Service Provider''
Procurement.Name is a auto number.

Please help!
James LoghryJames Loghry
Since Procurement__c's Name field is an auto number, it will never be "Test".  This system assert call is rather superfluous in that it doesn't assert much of anything.  You should just remove it.  

Instead, I would add one (or more) System.assert calls after Test.stopTest. In place of your System.assert, try adding the following after Test.stopTest:
System.assert(!pex.availableFields.isEmpty());

 
Kiran GKiran G
Hi James,

Thanks I have removed the below code from my test class

" insert objProcurement;
        objProcurement=[SELECT id,Name FROM Procurement__c WHERE id=:objProcurement.Id];
        System.assertEquals(objProcurement.Name,'Test'); " 

And inserted " System.assert(!pex.availableFields.isEmpty());" after Test.stop Test;

But Now I am getting a new error

"System.SObjectException: You cannot call reset when the data is being passed into the controller by the caller."

I am not much of a coder please help!
James LoghryJames Loghry
Can you please post your latest code?