You need to sign in to do that
Don't have an account?
rajubalaji
how to write a test class for if else condition and return values?
Hi evryone,
how to write test class for below code if any one know please help me and if possible please explain me.
public String getFieldByName(ProspectUser__c prospectUser, String fieldName ){
if(fieldName.equals('FirstName')){
return prospectUser.FirstName__c;
}else if(fieldName.equals('LastName')){
return prospectUser.LastName__c;
}else if(fieldName.equals('Gender')){
return prospectUser.Gender__c;
}else if(fieldName.equals('Email')){
return prospectUser.Email__c;
}else if(fieldName.equals('DateOfBirth')){
return String.valueOf(prospectUser.DOB__c.format());
}else if(fieldName.trim().equalsIgnoreCase('MemberId')){
return prospectUser.Member_ID__c;
}else{
return '';
}
}
thanks inadvance,
raju
how to write test class for below code if any one know please help me and if possible please explain me.
public String getFieldByName(ProspectUser__c prospectUser, String fieldName ){
if(fieldName.equals('FirstName')){
return prospectUser.FirstName__c;
}else if(fieldName.equals('LastName')){
return prospectUser.LastName__c;
}else if(fieldName.equals('Gender')){
return prospectUser.Gender__c;
}else if(fieldName.equals('Email')){
return prospectUser.Email__c;
}else if(fieldName.equals('DateOfBirth')){
return String.valueOf(prospectUser.DOB__c.format());
}else if(fieldName.trim().equalsIgnoreCase('MemberId')){
return prospectUser.Member_ID__c;
}else{
return '';
}
}
thanks inadvance,
raju
All Answers
You will have to satisfy all the conditions in order to cover if- else structure
Thank you so much for quick response.but here i was have one doubt instead of "ClassName obj1 = new ClassName(); " i was getting invaild type..can u please help me how to clear that.
thanks inadvance,
Raju
Apex Class:
public class UploadController {
public Account acc {get;set;}
public Boolean error {get;set;}
public String message {get;set;}
List<String> fieldList{get;set;}
public UploadController(ApexPages.StandardController controller) {
acc = [select Id, starId__c,Enviornment_Code__c from Account where Id = :((Account)controller.getRecord()).Id];
}
public String getFieldByName(ProspectUser__c prospectUser, String fieldName ){
if(fieldName.equals('FirstName')){
return prospectUser.FirstName__c;
}else if(fieldName.equals('LastName')){
return prospectUser.LastName__c;
}else if(fieldName.equals('Gender')){
return prospectUser.Gender__c;
}else if(fieldName.equals('Email')){
return prospectUser.Email__c;
}else if(fieldName.equals('DateOfBirth')){
return String.valueOf(prospectUser.DOB__c.format());
}else if(fieldName.trim().equalsIgnoreCase('MemberId')){
return prospectUser.Member_ID__c;
}else{
return '';
}
}
Test Class:
@isTest
public class TestUploadController {
static testMethod void testEligibilityUploadController(){Test.startTest();
Account acc = getAccount_Model2();
UploadController controller = new UploadController(new ApexPages.StandardController(acc));
controller.error = true;
controller.message ='text';
Test.StopTest();
}
static testmethod void testGetFieldByName(){
ProspectUser__c prop = new ProspectUser__c();
insert prop;
String result1 = patient.getFieldByName('FirstName');
String result2 = getFieldByName(prop , 'LastName');
String result3 = getFieldByName(prop , 'Gender');
String result4 = getFieldByName(prop , 'Email');
String result5 = getFieldByName(prop , 'DateofBirth');
String result6 = getFieldByName(prop , 'MemberId');
}
this is the apex class and test class which i written,actually i was new to coding.so can u please help me.
Thanks Inadvance,
Raju Balaji
i was facing issue what i need to write in requried field1.i was written like this but it was showing error.
Account acc =new Account();
acc.FirstName = 'nMM';
acc.LastName = 'MMM';
acc.Gender = 'Male';
acc.Email = 'raju+test@welldocinc.com';
acc.DateofBirth = '1/1/1980';
acc.Member_ID__c = '107556330459';
insert acc;
ProspectUser__c prop = new ProspectUser__c();
prop.FirstName = 'nMM';
prop.LastName = 'MMM';
prop.Gender = 'Male';
prop.Email = 'raju+test@welldocinc.com';
prop.DateofBirth = '1/1/1980';
prop.Member_ID__c = '107556330459';
insert prop;
can u please help me.
Thanks Inadvance,
Raju Balaji
Data in single quotes are considered as String Type . One more thing we usually dont have FirstName and LastName field on Account object unless its a Person Account which is enabled by Salesforce on request . By default the Account is of business type in the org.
As per your guide lines i have written code like this.but i was not incresing cose percentage.
static testmethod void testgetFieldByName(){
Account acc =new Account();
ProspectUser__c prop = new ProspectUser__c();
prop.FirstName__c = 'nMM';
prop.LastName__c = 'MMM';
prop.Gender__c = 'Male';
prop.Email__c = 'raju+test@welldocinc.com';
prop.DOB__c = System.today();
prop.Member_ID__c = '107556330459';
insert prop;
Test.startTest();
UploadController controller = new UploadController(new ApexPages.StandardController(acc));
String result1 = controller.getFieldByName(prop, 'FirstName');
String result2 = controller.getFieldByName(prop , 'LastName');
String result3 = controller.getFieldByName(prop , 'Gender');
String result4 = controller.getFieldByName(prop , 'Email');
String result5 = controller.getFieldByName(prop , 'DateofBirth');
String result6 = controller.getFieldByName(prop , 'MemberId');
Test.stopTest();
}
Thank you so much for help.
i have some chnages but ur guide will help me almost.
Thank you so much.