You need to sign in to do that
Don't have an account?
Manjunath reddy 25
How do we write a test class for the below class.
Need help in writing test class for the below class
public class GSDAccountteammembercontroller {
public list<HPE_Account_Team__c> listofrecords{get;set;}
public Boolean displayBOMPopUp {get;set;}
public Boolean TabToBeClosed {get;set;}
public PageReference SaveRecords()
{
system.debug('Method Entry');
//Id parentrecordid = ApexPages.currentPage().getParameters().get('Id');
Id parentrecordid = '001G000001IgfGZ';
list<HPE_Account_Team__c> insertlist = new list<HPE_Account_Team__c>();
system.debug('After the Method Entry');
for(HPE_Account_Team__c acctem :listofrecords)
{
system.debug('Entered the for condition'+acctem.Account__c);
if(acctem.Name__c!=null)
{
acctem.Account__c = parentrecordid;
insertlist.add(acctem);
system.debug('Method Entry'+insertlist);
}
}
try{
insert insertlist;
}
catch(Exception ex){
//ApexPages.addmessage(new Apexpages.Message(ApexPages.Severity.ERROR, ex.getMessage()));
ApexPages.addMessages(ex);
return null;
}
if(listofrecords!=null){
listofrecords.clear();
}
TabToBeClosed = true;
return null;
}
public GSDAccountteammembercontroller(){
displayBOMPopUp = false;
TabToBeClosed = false;
listofrecords = new list<HPE_Account_Team__c>();
for(Integer i=0; i<5; i++){
HPE_Account_Team__c gsdaccteam = new HPE_Account_Team__c();
gsdaccteam.Account__c=null;
List<PermissionSetAssignment> UserHasDCDMPerission = new List<PermissionSetAssignment>([SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId= :UserInfo.getUserId() AND PermissionSet.Name = 'GSDAccountDCDMRole']);
if(UserHasDCDMPerission.size() > 0)
gsdaccteam.recordtypeid = Schema.SObjectType.HPE_Account_Team__c.getRecordTypeInfosByName().get('DCDM').getRecordTypeId();
gsdaccteam.Name__c=null;
gsdaccteam.Role_Scope_Description__c='';
gsdaccteam.Role__c='';
listofrecords.add(gsdaccteam);
}
}
public void TestActFun(){
displayBOMPopUp = true;
}
public void TestPopUp(){
displayBOMPopUp = true;
}
public void closeBOMPopUp() {
displayBOMPopUp = false;
}
}
public class GSDAccountteammembercontroller {
public list<HPE_Account_Team__c> listofrecords{get;set;}
public Boolean displayBOMPopUp {get;set;}
public Boolean TabToBeClosed {get;set;}
public PageReference SaveRecords()
{
system.debug('Method Entry');
//Id parentrecordid = ApexPages.currentPage().getParameters().get('Id');
Id parentrecordid = '001G000001IgfGZ';
list<HPE_Account_Team__c> insertlist = new list<HPE_Account_Team__c>();
system.debug('After the Method Entry');
for(HPE_Account_Team__c acctem :listofrecords)
{
system.debug('Entered the for condition'+acctem.Account__c);
if(acctem.Name__c!=null)
{
acctem.Account__c = parentrecordid;
insertlist.add(acctem);
system.debug('Method Entry'+insertlist);
}
}
try{
insert insertlist;
}
catch(Exception ex){
//ApexPages.addmessage(new Apexpages.Message(ApexPages.Severity.ERROR, ex.getMessage()));
ApexPages.addMessages(ex);
return null;
}
if(listofrecords!=null){
listofrecords.clear();
}
TabToBeClosed = true;
return null;
}
public GSDAccountteammembercontroller(){
displayBOMPopUp = false;
TabToBeClosed = false;
listofrecords = new list<HPE_Account_Team__c>();
for(Integer i=0; i<5; i++){
HPE_Account_Team__c gsdaccteam = new HPE_Account_Team__c();
gsdaccteam.Account__c=null;
List<PermissionSetAssignment> UserHasDCDMPerission = new List<PermissionSetAssignment>([SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId= :UserInfo.getUserId() AND PermissionSet.Name = 'GSDAccountDCDMRole']);
if(UserHasDCDMPerission.size() > 0)
gsdaccteam.recordtypeid = Schema.SObjectType.HPE_Account_Team__c.getRecordTypeInfosByName().get('DCDM').getRecordTypeId();
gsdaccteam.Name__c=null;
gsdaccteam.Role_Scope_Description__c='';
gsdaccteam.Role__c='';
listofrecords.add(gsdaccteam);
}
}
public void TestActFun(){
displayBOMPopUp = true;
}
public void TestPopUp(){
displayBOMPopUp = true;
}
public void closeBOMPopUp() {
displayBOMPopUp = false;
}
}
First change this code in your class don’t hot code any class it won’t work in test class bellow line
Id parentrecordid = ApexPages.currentPage().getParameters().get('Id');
// Id parentrecordid = '001G000001IgfGZ';
Ask me any isuue above code .
Regards.
Harish.R.
Error: Compile Error: Constructor not defined: [GSDAccountteammembercontroller].<Constructor>() at line 23 column 64, If I comment that line code is getting saved, but code will not be covered.
you have to use because of no data will access from your org data base so if you are giving id it wilol not available in test class plot form . Add bellow code to your test class .
Regards ,
Harish.R.
Error: Compile Error: Constructor not defined: [GSDAccountteammembercontroller].<Constructor>() at line 38 column 68
public class GSDAccountteammembercontrollerTest{
private static testMethod void SaveRecordTest(){
account acc = new account();
acc.name='test';
insert acc;
contact con = new contact();
con.email = 'test@ayhoo.com';
con.Phone__c = '5423515415';
con.LastName ='last Name';
insert con;
HPE_Account_Team__c hpeAccountTeam = new HPE_Account_Team__c();
hpeAccountTeam.Account__c = acc.id;
hpeAccountTeam.Name__c = con.id;
hpeAccountTeam.Role__c = 'Other';
hpeAccountTeam.Role_Scope__c = 'Global';
hpeAccountTeam.Role_Scope_Description__c = 'testing';
insert hpeAccountTeam;
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles',UserName='testpermissionsetuser@testorg.com');
insert u;
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'AccountRating'];
apexpages.currentpage().getparameters().put('id' , acc.id);
PermissionSetAssignment psa = new PermissionSetAssignment();
psa.AssigneeId = u.Id;
psa.PermissionSetId = ps.Id;
insert psa;
System.runAs(u){
GSDAccountteammembercontroller teamMemberController = new GSDAccountteammembercontroller();
teamMemberController.SaveRecords();
}
}
}
what is methode is working in constracter .
Email me : harish.rao.salesforce@gmail.com
Regards ,
Harish.R.
It's working properly in my org test it in your org. i am created only 2 fields and one object but it will work in your org check and let me know any issue .
Regards ,
Harish.R.
harish.rao.salesforce@gmail.com
Did u get code coverage .
Regards ,
Harish.R
in line number 11
change
string parentrecordid = ApexPages.currentPage().getParameters().get('id');
please exectie my total class and test class once .
Downlode ypur code and copy past my code in your org . it will work .
Regards ,
Harish.
Update this code .
class
Test class
Add this add aded all things in this code
Any issue ask me immediatly .
Regards ,
Harish.R
error is
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, User Role Type is Required: [EmailPreferences]
Class.GSDAccountteammembercontrollerTest.SaveRecordTest: line 27, column 1
line 27 is
insert u;
Now test class is passing my code is as below, code coverage is 84%, but I need to increase another 2 percent.
if(parentrecordid != null && parentrecordid != '')
acctem.Account__c = parentrecordid ;
insertlist.add(acctem);
the above lines are not getting covered.
Add
Modify this lines upadte below code after
// insert hpeAccountTeam; line
Regards ;
Harish.R.
My test class is working fine now, I just need to cover the lines below from the main class.
if(acctem.Name__c!=null)
{
if(parentrecordid != null && parentrecordid != '')
acctem.Account__c = parentrecordid ;
insertlist.add(acctem);
}
what is the code coverage now .
Regards ,
Harish.R