You need to sign in to do that
Don't have an account?

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>();
system.debug('****'+selectedOwnerId);
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
Thanks
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>();
system.debug('****'+selectedOwnerId);
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
Thanks
Try out the below test class.
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.
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%.
@isTest
Public Class classfortesting{
static testmethod Void check_assigncontact(){
assigncontact a= new assigncontact();
Contact c= New Contact (LastName='aaa', Phone='987600089', Email='bhn.88@s.com');
insert c;
User u = new User(Alias='serk');
insert u;
a.getOptions();
a.processSelected();
}
}
whatelse needs to be written in order get atleast 80% code coverage.
Thnks
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.
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.