You need to sign in to do that
Don't have an account?
Ranadheer 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
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
All Answers
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
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);
}
}