+ Start a Discussion
Andrew Hoban 14Andrew Hoban 14 

Creating a test class for a contoller

Hi all, I was wondering if you could help me create a test class for the clontroller seen below. Any help would be appreciated,

Many thanks.
 
public class IncidentLogPopup
{ 

 public List<Incident_Log__c> memberList {get;set;}
    public List<Incident_Log__c> memberAddList {get;set;}
    public String memberName {get;set;} 
    public string searchstring {get;set;} 

    
    public PageReference cancel() {
        return null;
    }


    public IncidentLogPopup() {

    }
    
   
    public IncidentLogPopup(ApexPages.StandardController controller) {
        String sql = 'SELECT Match_Day_Ops__c, Date_Time__c, Date_Closed__c, Type__c, Priority__c, Incident__c, Reported_to__c, Reported_By__c, Opened_Closed_Status__c FROM Incident_Log__c';
        memberList = Database.Query(sql);
        memberAddList = new List<Incident_Log__c>();
        memberAddList.add(new Incident_Log__c(Match_Day_Ops__c = 'a0Gf000000111CV', Date_Time__c = DateTime.Now()));
         
        
    }
    
    public void AddRow()
    {
        memberAddList.add(new Incident_Log__c(Match_Day_Ops__c = 'a0Gf000000111CV', Date_Time__c = DateTime.Now()));
        
    } 
    
    Public Pagereference Save(){
        insert memberAddlist;
        PageReference page = new Pagereference('/a0G/o');
                     page.setRedirect(true);
            return page;
  }
   

        
   }

 
Best Answer chosen by Andrew Hoban 14
Anoop yadavAnoop yadav
Hi,

Try with the below code.
@isTest(seeAllData = true)
public class IncidentLogPopupTest(){
	public static testMethod void testMethod(){
		Incident_Log__c inc = new Incident_Log__c();
		inc.Name = 'Test Name';
		// Other mandatory field
		insert inc.
		
		ApexPages.StandardController sc = new ApexPages.StandardController(inc);
		IncidentLogPopup incPop1 = new IncidentLogPopup(sc);
		IncidentLogPopup incPop2 = new IncidentLogPopup();
		incPop1.addRow();
		incPop1.save();
		
	}
}

 

All Answers

Andrew Hoban 14Andrew Hoban 14
Thanks for your reply. i am still stuggling on how to start my test class. For example, the add row i am unsure how I would create this in the test class. Many thanks. 
Anoop yadavAnoop yadav
Hi,

Try with the below code.
@isTest(seeAllData = true)
public class IncidentLogPopupTest(){
	public static testMethod void testMethod(){
		Incident_Log__c inc = new Incident_Log__c();
		inc.Name = 'Test Name';
		// Other mandatory field
		insert inc.
		
		ApexPages.StandardController sc = new ApexPages.StandardController(inc);
		IncidentLogPopup incPop1 = new IncidentLogPopup(sc);
		IncidentLogPopup incPop2 = new IncidentLogPopup();
		incPop1.addRow();
		incPop1.save();
		
	}
}

 
This was selected as the best answer
Andrew Hoban 14Andrew Hoban 14
Thanks for your help! With this code I am getting the compile error: "expecting a semi-colon, found 'ApexPages.StandardController' at line 9 column 8"

Thanks
Anoop yadavAnoop yadav
Hi,

USE "insert inc;" at Line 7 insted of insert inc.
Andrew Hoban 14Andrew Hoban 14
thanks mate that has got me 89% code coverage and i have learnt from your help. Many thanks.