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
MubarakMubarak 

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
Best Answer chosen by Mubarak
ManojjenaManojjena

Hi Mubarak,

Try to add default constructor to your class as below .

public contactController(){}

And try with below test class it will give you 100% coverage in case any issue just add mandatory field to Account and Contact .

 

@isTest
public class TestcontactController{
  public static TestMethod void unitTest(){
    Account acc=new Account();
    acc.Name='Test';
    insert acc;
	acc=[SELECT id,Name FROM Account WHERE id =:acc.Id];
	System.assertEquals(acc.Name,'Test');
    Contact cont=new Contact();
    cont.LastName='TestContact';
    insert cont;
	con=[SELECT id,LastName FROM Conatct WHERE id =:cont.Id];
	System.assertEquals(con.LastName,'TestContact');
    System.currentPageReference().getParameters().put('id',cont.Id);
    contactController contrll=new contactController();
    contrll.getContact();
    contrll.getContacts();
  }
}
Thanks 
Manoj

All Answers

ManojjenaManojjena

Hi Mubarak,

Try to add default constructor to your class as below .

public contactController(){}

And try with below test class it will give you 100% coverage in case any issue just add mandatory field to Account and Contact .

 

@isTest
public class TestcontactController{
  public static TestMethod void unitTest(){
    Account acc=new Account();
    acc.Name='Test';
    insert acc;
	acc=[SELECT id,Name FROM Account WHERE id =:acc.Id];
	System.assertEquals(acc.Name,'Test');
    Contact cont=new Contact();
    cont.LastName='TestContact';
    insert cont;
	con=[SELECT id,LastName FROM Conatct WHERE id =:cont.Id];
	System.assertEquals(con.LastName,'TestContact');
    System.currentPageReference().getParameters().put('id',cont.Id);
    contactController contrll=new contactController();
    contrll.getContact();
    contrll.getContacts();
  }
}
Thanks 
Manoj
This was selected as the best answer
Parthiban sfdcParthiban sfdc
Hi Try this

@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();

}

}