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
Supraja KallakuriSupraja Kallakuri 

Code coverage is 73% How can I improve it?

code coverage is 73% What can I do to improve it?I was trying o deploy  my code to the production org and failed as the code coverage is below 75%. Can someone suggest a way to improve my code coverage? Thanks!


public with sharing class contrller {
Public id Current_Acc_Id;

    public contrller(ApexPages.StandardController controller) {
    Current_Acc_Id = controller.getRecord().id;

    public List<Contact> getrelatedContacts(){
        List <contact> conList = New List<Contact>();
        for(Account acc:[select id,name,(select name,email, phone, LastModifiedDate from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
        return conList;

Test Class

private class Contrller_Test2{
  static void setupData(){
    Account testAccount = new Account(Name = 'Test', Chainname__c = '123',  Chain_Code__c ='456', Company__c = '789');
    insert testAccount;
    Contact testcontact = new Contact ( LastName = 'Test', Account = testAccount);
    insert testcontact;
  static testMethod void getrelatedContacts(){
    Account myAccount = [SELECT Id, Name FROM Account WHERE Name = 'Test' ];
     List <contact> myconList = New List<Contact>();
        for(Account acc:[select id,name,(select name,email, phone, LastModifiedDate from contacts) from account where]){
           for(contact con:acc.contacts)
               system.assertequals(1, myconlist.size());
Actually you have zero coverage for your test class. Also you are not calling the setupData() in your test method.

below is the code that should be in your testmethod. replace <yourVF> with your VF page.
  PageReference pageRef = Page.<yourVF>;

  Account myAccount = [SELECT Id, Name FROM Account WHERE Name = 'Test' ];

  ApexPages.StandardController stdController = new ApexPages.StandardController(myAccount);
  contrller controller = new contrller (stdController);
  List <contact> myconList = controller.getrelatedContacts();


  system.assertequals(1, myconlist.size());

you can refer the link below on how to test controller and controller extn