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

Test class
public class contactController {
public List<Contact> getContacts() {
return [SELECT Id,Name, Account.Name, Phone, Email
FROM Contact
ORDER BY LastModifiedDate DESC LIMIT 10];
}
public Contact getContact() {
Id id = System.currentPageReference().getParameters().get('id');
return id == null ? new Contact() : [SELECT Id, Name
FROM Contact
WHERE Id = :id];
}
}
Hi all,
I wrote a test class for this class but the code coverage is only 10%,I tried so many times but it shows only 10%.can any one provide me test class for the above class.
Thanks
public List<Contact> getContacts() {
return [SELECT Id,Name, Account.Name, Phone, Email
FROM Contact
ORDER BY LastModifiedDate DESC LIMIT 10];
}
public Contact getContact() {
Id id = System.currentPageReference().getParameters().get('id');
return id == null ? new Contact() : [SELECT Id, Name
FROM Contact
WHERE Id = :id];
}
}
Hi all,
I wrote a test class for this class but the code coverage is only 10%,I tried so many times but it shows only 10%.can any one provide me test class for the above class.
Thanks
Hi Mubarak,
Try to add default constructor to your class as below .
public contactController(){}
ThanksAnd try with below test class it will give you 100% coverage in case any issue just add mandatory field to Account and Contact .
Manoj
All Answers
Hi Mubarak,
Try to add default constructor to your class as below .
public contactController(){}
ThanksAnd try with below test class it will give you 100% coverage in case any issue just add mandatory field to Account and Contact .
Manoj
@istest
Private class TestcontactController{
@testsetup
static void TestsetupMethod(){
Account acc= new Account();
acc.name = 'Account1';
insert acc;
contact con =new contact();
con.lastname = 'Contactt1';
con.accountId = acc.id;
insert con;
}
@istest
static void TestMethod1(){
contactController concatcontroller =new contactController();
Test.StartTest();
list<Contact> contactList = concatcontroller.getContacts();
Test.StopTest();
system.assert(contactList != null);
system.assert(contactList.size() > 0);
}
@istest
static void TestMethod2(){
contactController concatcontroller =new contactController();
contact con =[select id,lastname from contact];
System.currentPageReference().getParameters().put('id',con.id);
Test.StartTest();
contact contact1 =concatcontroller.getContact();
Test.StopTest();
}
@istest
static void TestMethod3(){
contactController concatcontroller =new contactController();
Test.StartTest();
contact con = concatcontroller.getContact();
Test.StopTest();
}
}