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
Ranadheer chRanadheer ch 

How to cover the below class line in my testclass

My class

public with sharing class AccountContactRoles {
    private final Contact acr;
 
    public AccountContactRoles(ApexPages.StandardController controller){
        this.acr = (Contact)controller.getRecord();
    }
 
    public List<AccountContactRole> acrs {
        get {
            if (acrs == null) {
                acrs = [Select Id, AccountId, ContactId, Role, IsPrimary From AccountContactRole
                    Where ContactId=:acr.Id];             
            }
            return acrs;
        }
        private set;
    }
}


my Test class

@isTest
private class TestAccountcontactroleClass{
    @isTest
    private static void testClass()
    {
    //Standard controller of Accountcontactrole
    //Create a new instance of Accountcontactrole
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    Contact con = new Contact(LastName = 'Test Last Name', AccountId = acc.Id);
    insert con;
 
    AccountContactRole acr1 = new AccountContactRole();
                acr1.AccountId = acc.Id;
                acr1.ContactId = con.Id;
                acr1.Role = 'Test 1';
                acr1.IsPrimary=True;
            insert acr1;
    //Insert the object virtually


    //Create a new instance of standard controller
    ApexPages.StandardController sc = new ApexPages.standardController(con);

    AccountContactRoles controller = new AccountContactRoles(sc); 
    }
}




Here the problem is am getting only 57% code coverage ...so iwant to cover the below lines in test class...how to write a methosd in test class to cover those lines...the uncoverd  lines in my test class from class is  

public List<AccountContactRole> acrs {
        get {
            if (acrs == null) {
                acrs = [Select Id, AccountId, ContactId, Role, IsPrimary From AccountContactRole



The above class line is not coverd in my test class...ple help me ...thanks  in advance
Best Answer chosen by Ranadheer ch
Fabien TaillonFabien Taillon
It's because the line you added should be the last one, just after AccountContactRoles controller = new AccountContactRoles(sc);

All Answers

Fabien TaillonFabien Taillon
You should just call it. Something like:
List<AccountContactRole> acrsTest = controller.acrs;

Also, you should encapsulate the following lines between Test.startTest() and Test.StopTest():
ApexPages.StandardController sc = new ApexPages.standardController(con);
AccountContactRoles controller = new AccountContactRoles(sc);

This is because you don't want the creation of your test data to count over your test governor limits.
You can find more on Test classes here :
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Ranadheer chRanadheer ch
Thanks fab i invoked the method in controller...then am getting below error..

Error: Compile Error: Variable does not exist: controller.acrs at line 20 column 41

below is my code after i invoked the method

@isTest
private class TestAccountcontactroleClass{
    @isTest
    private static void testClass()
    {
    //Standard controller of Accountcontactrole
    //Create a new instance of Accountcontactrole
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    Contact con = new Contact(LastName = 'Test Last Name', AccountId = acc.Id);
    insert con;
   
    AccountContactRole acr1 = new AccountContactRole();
                acr1.AccountId = acc.Id;
                acr1.ContactId = con.Id;
                acr1.Role = 'Test 1';
                acr1.IsPrimary=True;
            insert acr1;
    //Insert the object virtually

    List<AccountContactRole> acrsTest = controller.acrs;
  

    //Create a new instance of standard controller
   
    ApexPages.StandardController sc = new ApexPages.standardController(con);

    AccountContactRoles controller = new AccountContactRoles(sc);   
    }
}
Fabien TaillonFabien Taillon
It's because the line you added should be the last one, just after AccountContactRoles controller = new AccountContactRoles(sc);
This was selected as the best answer
Ranadheer chRanadheer ch
Thank you so much fab...it really helps me...thanks ya....i will defenatly remeber ur name ...now i got 100% coverage..thanks