+ Start a Discussion
brielea1984brielea1984 

Help Writing Test Class

Hello,

 

I'm a novice in writing Apex classes, and copied some code I found on these boards. I need to write a test class to increase my code coverage and am incredibly unfamilliar with the language. Any help would be greatly appreciated.

 

public class ContactEntry {

    public List<Contact> con {get; set;}

    public ContactEntry(ApexPages.StandardController myController) {
        con = new List<Contact>();
        con.add(New Contact());}

    public void addrow() {
        con.add(new Contact());}
            
    public void removerow(){
        Integer i = con.size();
        con.remove(i-1);}
            
    public PageReference save() {
        insert con;
        //PageReference home = new PageReference('/home/home.jsp');
        //home.setRedirect(true);
        //return home; }}
        PageReference conlist = new PageReference('/apex/contactlist');
        conlist.setRedirect(true);
        return conlist; }}

 Thank you in advance!

Hengky IlawanHengky Ilawan

Hi,

 

There is a good article about unit test class here.

 

You may want to take a look at Salesforce own documentation:

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests

 

Regards,

Hengky

vishal@forcevishal@force

Hi, as suggested the best place to start learning about test coverages would be salesforce documentation.

 

For now, you should atleast be aware why you need test coverage and how to do that!

 

WHY?

To ensure that the code you've written is getting used, ie, it isn't unnecessary for your functionality.

 

HOW?

You write apex to perform some operation/functionality, so write test coverage in such a way that your code has to behave the way it is expected to.

 

Example:

 

I have this small class

 

public class deleteAccount()

{

     public deleteAccount()

     {}

 

     public void deleteAcc(Id idToBeDeleted)

     {

          Account a = [Select Id From Account Where Id = : idToBeDeleted];

          delete a;

     }

}

 

Your class simply deletes an account based on it's id.

 

So you write your test coverage this way:

 

Account a = new Account(Name = 'test');

insert a; // you create a test record for your test class

 

@isTest

private class testDeleteAccount

{

     private static void del()

     {

          deleteAccount del = new deleteAccount();

          del.deleteAcc(a.Id); // now you are calling your class method, this is what you mean by code-coverage.

     }

}

vishal@forcevishal@force

Btw, this code should help you for your class.

 

@isTest

private class testContactEntry

{

     private static void testCE()

     {

          Contact c = new Contact(LastName = 'test'); // add other required field, if any

          insert c;

 

          ApexPages.standardController std = new ApexPages.standardController(c);

 

          ContactEntry objCE = new ContactEntry(std);

 

          objCE.addrow();

 

          obj.removerow();

 

          obj.save();

     }

}

 

 

NOTE : your Contact List con has contacts without LastNAme (a required field), so it will throw error on save.