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
Shephali SwarnkarShephali Swarnkar 

How to write Test methods

Hi All,
     Below is my controller code how to write test Method for it:
public with sharing class assigncontact
    public List<ContactWrapper> conWrp { get; set; }
    public Boolean showContact { get; set; }
    public Id selectedOwnerId { get; set; }
public assigncontact()
        conWrp = new List<ContactWrapper>();
        showContact = true;
        for( Contact c : [ Select Id, Name, Email, Phone, ACCOUNT.NAME, Owner.Name from Contact order by Name])
            conWrp.add( new ContactWrapper( c ));
    public List<SelectOption> getOptions()
        showContact = false;
        List<SelectOption> options = new List<SelectOption>();
        for( User b : [ Select Id, Name from User where IsActive = true order by Name ])
            options.add( new SelectOption( b.Id, b.Name ));
        return options;
 public void processSelected()
        List<Contact> contacts = new List<Contact>();
        for( ContactWrapper wrp : conWrp )
            if( wrp.isSelected )
                Contact c = new Contact( Id = wrp.accn.Id, OwnerId = selectedOwnerId );
                contacts.add( c );
        if( contacts.size() > 0 )
            update contacts;
    public class ContactWrapper
        public Contact accn { get; set; }
        public Boolean isSelected { get; set; }     

        public ContactWrapper( Contact a )
            this.accn = a;
            this.isSelected = false;

Please Help
Arunkumar RArunkumar R
Hi Arpit,

Try out the below test class. 
private class AssignContactTest
    static testMethod void insertContact()
       // Assign all mandatory field values.
        Contact cnt = new Contact();
        cnt.LastName = 'TEst Contact';
        insert cnt;
        assigncontact ac = new assigncontact();
        ac.conWrp[0].isSelected = true;
        ac.selectedOwnerId = Userinfo.getUserId();


1. Ensure you mentioned all madatory field values in test class for contact insertion.
2. Based on your logic, change the test class and put system.assertion to verify the behaviour working properpely.
Shephali SwarnkarShephali Swarnkar
Hi ArunKumar,

        I have just started my condings for test methods and as a beginner i have written a below. it is running successfully but still showing code coverage as 0%.

  Public Class classfortesting{
       static testmethod Void check_assigncontact(){
       assigncontact a= new assigncontact();
        Contact c= New Contact (LastName='aaa', Phone='987600089', Email='');
        insert c;
        User u = new User(Alias='serk');
        insert u;        

whatelse needs to be written in order get atleast 80% code coverage.

Arunkumar RArunkumar R
Hi Shephali Swarnkar,

My code which posted above give 100% coverage. Please try that one, before executing test class ensure the following,

1. Setup --> Apex Classes --> Compile all classes.
2. Setup --> Apex Test Execution --> View Test History --> Clear Test Data
3. Finally, Run the test class which you have created.
Shephali SwarnkarShephali Swarnkar
Hi Arun,
    Thanks for Reply. I follwed the above mentioned steps in order to Run the test class and i am getting error but unable tofigure it out.Please Help.
User-added image