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
Mohammed ArshadMohammed Arshad 

Need help in writing a test class for the below code

Apex Class:-
public class Insurance_List{
    public String id {get;set;}
    public Insurance__c insurance {get;set;}
public Insurance_List(ApexPages.StandardController controller) {
    id = ApexPages.currentPage().getParameters().get('id');
    insurance = [select Code__c,ShortName__c,LongName__c,MinValue__c,MaxValue__c,CheckDigitRule__c from Insurance__c where Id = :id];     
    }
}
Test Class:-
@isTest
public class Insurance_List_Test {

    static testMethod void methodOne(){
           
           Application__c app = TestDataBuilder.createApplication();             
           app.Response_Code__c = 'New';
           insert app;
           System.assertNotEquals(null,app.Id);//assert inserted Application__c
           List<Insurance__c> insurances = new List<Insurance__c>();
           Insurance__c insuranceList =TestDataBuilder.createInsurance(app.Id);
           insuranceList.Code__c = '99999';
           insuranceList.ShortName__c = 'Test';
           insuranceList.LongName__c= 'Test';
           insuranceList.MinValue__c= 200;
           insuranceList.MaxValue__c= 1000;
           insuranceList.CheckDigitRule__c='123';     
           dl.add(insuranceList);
           insert insurances;
           ApexPages.StandardController standardController = new ApexPages.StandardController(insuranceList);           
           APP_Disbursement_List appDisbList = new APP_Disbursement_List(standardController);
    }   
}
Error in Test History:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Application status already Accepted. You are no longer allowed to insert a record.: []
GhanshyamChoudhariGhanshyamChoudhari
maybe because your trigger/validation rule on Insurance__c conflicting with test class.
Mohammed ArshadMohammed Arshad
Hi Deve, 

Thanks for the response...
Sorry i realised that it was typo error it is actually "insurances.add(insuranceList)"

Updated Test:

Test Class:-
@isTest
public class Insurance_List_Test {

    static testMethod void methodOne(){
           
           Application__c app = TestDataBuilder.createApplication();             
           app.Response_Code__c = 'New';
           insert app;
           System.assertNotEquals(null,app.Id);//assert inserted Application__c
           List<Insurance__c> insurances = new List<Insurance__c>();
           Insurance__c insuranceList =TestDataBuilder.createInsurance(app.Id);
           insuranceList.Code__c = '99999';
           insuranceList.ShortName__c = 'Test';
           insuranceList.LongName__c= 'Test';
           insuranceList.MinValue__c= 200;
           insuranceList.MaxValue__c= 1000;
           insuranceList.CheckDigitRule__c='123';     
           insurances.add(insuranceList);
           insert insurances;
           ApexPages.StandardController standardController = new ApexPages.StandardController(insuranceList);           
           Insurance_List insuranceList = new Insurance_List(standardController);
    }   
}
Error in Test History:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Application status already Accepted. You are no longer allowed to insert a record.: []
devedeve
Hi Mohammed,

Please check validation rule on Insurance__c object and also check are you setting application status value in this method TestDataBuilder.createInsurance(app.Id);
Mohammed ArshadMohammed Arshad
Hi Deve,

I found that there is a Trigger written on Insurance__c object and the error message comes from there:

Error: Application status already Accepted. You are no longer allowed to insert a record.

Trigger Code: 
TriggerHandler accessControl = new TriggerHandler();
if(Trigger.isBefore){
        //before insert
        if(Trigger.isInsert){
            //handler.saveInsurance(trigger.new);
            for(Insurance__c d: Trigger.new){
                Boolean flag = true;
                flag = accessControl.insertInsurance(d.Response_Code__c);
                if(!flag){
                    d.addError('Application status already Accepted. You are no longer allowed to insert a record.');
                }
            }

Apex Class:(TriggerHandler)
public Boolean insertInsurance(String responseCode){
        Boolean flag = true;
        try{
            System.debug('!@#TriggerHandler.insertInsurance.responseCode: '+responseCode);
            flag = Response_Code_Control__c.getInstance(responseCode).Save_Insurance_Standard__c;
            System.debug('!@#TriggerHandler.insertInsurance.flag: '+flag);
        }catch(Exception e){
            System.debug('!@#TriggerHandler.insertInsurance.e: '+e.getMessage() + ':' + e.getStackTraceString());
            flag = false;
        }
        return flag;
    }
 
devedeve
Yes Mohammed but still you have to check vaildation rule on Insurance__c object because it showing FIELD_CUSTOM_VALIDATION_EXCEPTION error which is due to some validation.
Can you please provide this debug value
'System.debug('!@#TriggerHandler.insertInsurance.e: '+e.getMessage() + ':' + e.getStackTraceString());'