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
deepakMdeepakM 

unable to get the test coverage upto 75% of apex class.

hi everyone

 

From last fews days i am trying to deploy a class with good test coverge but i unable to achieve that.

 

pleas help me to get the best code coverage as i tried to fix all the point in test case but even though i fail to do so.

 

 

My apex class is shown below and red lines are not coverd.

 

public class FFPage {

FF__c FF;
public List<Opportunity> listOpp;
public String Id {get; set;}
public Opportunity Opp {get; set;}
public String Msg {get; set;}
public boolean chkConfirm {get;set;}
public boolean chkAcct {get;set;}
public static boolean isApexTest = false;
//boolean flag = false;
public FFPage(ApexPages.StandardController stdController)
{
FF = (FF__c)stdController.getRecord();
Id = FF.Opportunity__c;

}

public PageReference Save()
{
chkConfirm = false;
chkAcct = false;
Msg = Apexpages.currentPage().getParameters().get('one');
System.debug('*************Msg************ : ' + Msg );


try
{

if(FF.Id == null)
{
string obj = main(FF.Opportunity__c,FF.Id,FF.FFGuid__c,FF.Account_Number__c,FF.Account_Type__c,FF.Last_Name__c,FF.Miles__c,FF.Password__c,'add',Msg );
// if(obj.contains('success'))
System.debug('*************RailMsgobj*********'+obj);
if(obj.contains('success')|| obj=='success')
{
System.debug('*************Railsobj*********'+obj);
string[] parts = obj.split(',');
if(parts.size()>1)
{
FF.FFGuid__c = parts[1];
}
System.debug('*************Guid*********'+FF.FFGuid__c);
insert FF;
System.debug('insert');
// ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, parts[0]));
PageReference pr = new PageReference('https://c.cs9.visual.force.com/apex/FFPage?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
pr.setRedirect(true);
pr.getParameters().put('paramName', 'Saved Successfully');
return pr;

}
else
{
System.debug('notinsert');
// obj == 'Not enough miles in account';
//chkConfirm = true;
if(obj.contains('Not enough miles in account')|| obj=='Not enough miles in account')
{
chkConfirm = true;
}
else if(obj.contains('Account does not exist')|| obj=='Account does not exist')
{
System.debug('*************AccountNot*********'+obj);
chkAcct = true;
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING,obj));
}


}

}
else
{
string userID;
userID = userinfo.getProfileId();
userID = userID.substring(0,15);
System.debug('*************LoginuserID *********'+userID );
Decimal miles;
if(userID != '00ed0000000vQrA')
{

Opportunity opp = [SELECT id, StageName FROM Opportunity WHERE Id = :FF.Opportunity__c];

if(opp .StageName == 'Completed')
{
FF__c ffobj = [SELECT id, Miles__c FROM FF__c WHERE Id = :FF.id];
if(ffobj != null)
{
if(ffobj.Miles__c != null)
{
miles = ffobj .Miles__c;
}
}
}
else
{
miles = FF.Miles__c;

}

}
else
{
miles = FF.Miles__c;

}
System.debug('*************NewMiles*********'+miles );

string obj = main(FF.Opportunity__c,FF.Id,FF.FFGuid__c,FF.Account_Number__c,FF.Account_Type__c,FF.Last_Name__c,miles,FF.Password__c,'update',Msg);
if(obj.contains('success')|| obj=='success')
{
string[] parts = obj.split(',');
if(parts.size()>1)
{
FF.FFGuid__c = parts[1];
}
System.debug('*************Guidupdate*********'+FF.FFGuid__c);
try
{
update FF;
System.debug('update');
//ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, obj));
PageReference pr = new PageReference('https://c.cs9.visual.force.com/apex/FFPage?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
pr.setRedirect(true);
pr.getParameters().put('paramName', 'Updated Successfully');
return pr;

}
catch(Exception e)
{
string expmsg = e.getMessage();
System.debug(expmsg);
if(expmsg.contains('FIELD_CUSTOM_VALIDATION_EXCEPTION, Opportunity is closed,You cannot edit the MIles.'))
{

System.debug('DmlException');
// ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Opportunity is closed,You cannot edit the MIles'));
PageReference pr = new PageReference('https://c.cs9.visual.force.com/apex/FFPage?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
pr.setRedirect(true);
pr.getParameters().put('paramName', 'Opportunity is closed,You cannot edit the MIles');
return pr;
}
}

}
else
{
System.debug('notupdate');
if(obj.contains('Not enough miles in account')|| obj=='Not enough miles in account')
{
chkConfirm = true;
}
else if(obj.contains('Account does not exist')|| obj=='Account does not exist')
{
chkAcct = true;
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, obj));
}
//PageReference pr = new PageReference('https://c.na14.visual.force.com/apex/FFPage?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
// pr.setRedirect(true);
// pr.getParameters().put('paramName', obj);
// return pr;
}



}



}

catch(DmlException ex)
{

}
return null;
}
public String main(String opid,String ffid,String guid,String actnu,String acttype,String lastn,Decimal miles,String pwd,String action,String Chkmiles)
{

SalesForceUrl__c sf = SalesForceUrl__c.getValues('AMUrl');
SalesForceUrl__c sf2 = SalesForceUrl__c.getValues('IsSandBox');
// Do a whole bunch of stuff

// Build the http request
HttpRequest req = new HttpRequest();
Http h = new Http();

if( sf != null && sf2!= null)
{
req.setEndpoint(sf.SettingValue__c+'/salesforce/test?oppid='+ opid+'&ffid='+ffid+'&guid='+guid +'&acctnu='+actnu +'&accttype='+acttype+'&lastname='+lastn+'&miles='+miles+'&pwd='+pwd+'&actionType='+action+'&IsSandbox='+sf2.SettingValue__c+'&Chkmiles='+Msg);

}
req.setMethod('GET');

// Invoke web service call
String result = '';
if (!isApexTest){
// Make a real callout since we are not running a test
HttpResponse res = h.send(req);
result = res.getBody();
System.debug('Result=' +result);
System.debug('STATUS:'+res.getStatus());
} else {
// A test is running
result = 'success';
}

// Do whole bunch of stuff

return result;
}


// Wrapper method for "main" that we will call in the Test Class
public String mainForTest(){
isApexTest = true;
DateTime dT = System.now();
Decimal i = 1.2;
return main('006d0000007jfwO','a00d000000560NZ','a00d000000560NZ','11','AA','aa',i,'asd','add','true');
}
public PageReference Cancel()
{

PageReference pr = new PageReference('/' + Id);
pr.setRedirect(true);
return pr;
}
}

 

 

 

 

MY Test Class is 

 

 

 

@isTest
private class FFPageTestCase {

static testMethod void myUnitTest() {
FF__c FF;
List<Opportunity> listOpp;
List<FF__c> listFF;
String Id;
String result;
String webresult;
String webresultu;

string obj = '';
List<String> parts;


Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.StageName = 'Ticketing';
opp.p1_First_Name__c ='A';
opp.p1_Last_Name__c ='D';
opp.p1_Gender__c = 'S';
opp.p1_First_Name__c ='S';
opp.p1_Last_Name__c ='A';
opp.p1_Gender__c = 'D';
opp.DOB_Main_Passenger__c = System.now().date();
opp.Number_of_Passengers__c = 4;
opp.CloseDate = System.now().date();
opp.Departure_Date__c = System.now().date();
opp.Origin_City__c = 'DEL';
opp.Destination_City__c= 'MUI';
opp.Return_Date__c = System.now().date();
opp.Override_Itinerary_Builder__c = true;
opp.Complete_Itinerary_Information__c ='test';
opp.Approved__c ='Test1';
opp.AmMessage__c ='success';
insert opp;

FF = new FF__c();
FF.Account_Number__c ='1234456';
FF.Account_Type__c = 'ANA';
FF.DOB__c = system.today();
FF.Opportunity__c = opp.Id;
FF.Last_Name__c = 'Dutt';
FF.Miles__c = 1.2;
FF.Password__c= 'asd';
FF.Mileage_Cost__c = 0.0210;
FF.FFGuid__c ='hhhhdofhff';
webresult = 'success';
obj = 'Success,785765876';
parts = obj.split(',');
if(parts.size()>1)
{
FF.FFGuid__c = parts[1];
}

insert FF;
opp.StageName = 'FF Account Assigned';
update opp;


webresultu = 'success';
update FF;


try
{
ApexPages.StandardController sc = new ApexPages.StandardController(FF);
FFPage sc1 = new FFPage(sc);


String retVal = sc1.mainForTest();
// Add asserts for validation
String expectedResult = 'success';
System.assertEquals(retVal, expectedResult);
sc1.Save();


FF__c FF2 = new FF__c();
FF2.Account_Number__c ='1234456';
FF2.Account_Type__c = 'ANA';
FF2.DOB__c = system.today();
FF2.Opportunity__c = opp.Id;
FF2.Last_Name__c = 'Dutt';
FF2.Miles__c = 1.2;
FF2.Password__c= 'asd';
FF2.Mileage_Cost__c = 0.0210;
FF.FFGuid__c ='hhhhdofhff';
webresult = 'success';
insert FF2;

SalesForceUrl__c sf = new SalesForceUrl__c();
sf.Name ='AMUrl';
sf.SettingValue__c ='test';
insert sf;
SalesForceUrl__c sf2 = new SalesForceUrl__c();
sf2.Name ='IsSandBox';
sf2.SettingValue__c ='true';
insert sf2;
sc1.Cancel();
}

catch(System.CalloutException e)
{
webresult = e.getMessage();
}
System.assertEquals(webresult,'success');

}
}

 

 

 

 

Please help me to get rid of this.

 

thanks

 

 

Niket SFNiket SF

Hello Dipak,

 

   You need write more test methods to pass the senarios. I think you can write a class which wil handle all the test data creation method. In each and every method you just need to call the those methods for data creation. And just change the senarios.

 

 

 

deepakMdeepakM

@nik : try to give me some example to explain .

Niket SFNiket SF

Hello Deepak,

 

 According to me you have written a extension and in you condition you are checking the 

if(FF.Id == null)  

 

 and in your test method you have inserted "insert FF". So this condtion will not work. Please make sure that this condition is working practically.

 

 

Skype Name : niket.chandane

deepakMdeepakM

@nik

 

i did that in same test class  but not effect any thing.

 

static testMethod void myUnitTest02()
{
FF__c FF;
try
{
if(FF != null)
{
ApexPages.StandardController sc = new ApexPages.StandardController(FF);
FFPage sc1 = new FFPage(sc);
}

}

catch(System.CalloutException e)
{

}
}

 

 

neophyteneophyte

Try this :

 

ApexPages.StandardController sc = new ApexPages.StandardController(FF);
FFPage sc1 = new FFPage(sc);

sc1.FF.field1__c = 'test';

sc1.FF.field2__c = 'test'; // Add all field values which are needed

sc1.save();

 

-Anand