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
Andrew Hoban 6Andrew Hoban 6 

Issue creating Test Class

Hi all,

I was wondering if anyone could provide a test class for the class provided below. 

Thanks
 
public class IncidentLogPopup{ 
    public List<Incident_Log__c> incidentLogList { get; set; }
    public List<Incident_Log__c> memberAddList { get; set; } //remove this
    public Boolean isValidState { get; set; }
    public String memberName {get;set;} 
    public string searchstring {get;set;} 
    
    private Id matchDayOpsId;
    
    public PageReference cancel(){
        return new PageReference('/'+matchDayOpsId);
    }

    private IncidentLogPopup(){}
   
    public IncidentLogPopup(ApexPages.StandardController controller) {
        isValidState=false;
        
        try {
            if (!ApexPages.CurrentPage().getParameters().containsKey('Id')) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Parameters.'));
                return;
            }
            
            matchDayOpsId = ApexPages.CurrentPage().getParameters().get('Id');        
            
            String q = 'SELECT Match_Day_Ops__c, Date_Time__c, Date_Closed__c, Type__c, Incident_Closed__c,';
            q+='Priority__c, Incident__c, Reported_to__c, Reported_By__c, Opened_Closed_Status__c';
            q+=' from Incident_Log__c';
            q+=' where Match_Day_Ops__c=\''+matchDayOpsId+'\'';
            
            incidentLogList = Database.Query(q);
            
            if (incidentLogList.size()==0){
                incidentLogList.add(new Incident_Log__c(Match_Day_Ops__c = matchDayOpsId, Date_Time__c = DateTime.Now()));  
            }
            isValidState=true;
        } catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
        }
    }
    
    public Pagereference addRow(){
        incidentLogList.add(new Incident_Log__c(Match_Day_Ops__c = matchDayOpsId, Date_Time__c = DateTime.Now()));   
        return null;
    } 
      
    Public Pagereference quickSave(){
        upsert incidentLogList;           
        return null;
    }
    
    public PageReference save(){ return null;} //remove this
   }

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
@isTest
public class IncidentLogPopupTest
{
    static testMethod void IncidentLogPopupTest()
	{
		Account acct = new Account();
			acct.Name = 'Salesforce.com';
			acct.Industry = 'Technology';
		insert acct;
        // Add all Test Data Here
		Incident_Log__c inLog = new Incident_Log__c();
			//inLog.Date_Time__c = System.today();
			inLog.Date_Time__c = DateTime.Now();
			//insert all Record Fields
		insert inLog;
		Test.StartTest();
			ApexPages.StandardController controllerer = new ApexPages.standardController(inLog);
			IncidentLogPopup testClass= new IncidentLogPopup(controllerer);
			testClass.addRow();
			testClass.quickSave();
			testClass.save();
		Test.StopTest();
    }
}

Please create test data in above test class.
For VF page we need to use below code in Test classes

            ApexPages.StandardController controllerer = new ApexPages.standardController(inLog);
            IncidentLogPopup testClass= new IncidentLogPopup(controllerer);

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help