+ Start a Discussion
deepakMdeepakM 

Need A Test case for the class that will used for component

hi everyone 

 

i have one class below which is used as a component,but i unable to deploy due to code coverage.

 

Apex Class :

 

public with sharing class ItineraryBuilderController {
public List<Test_Flight_Request__c> listFR;
public Id opportunityID;
public string result;
public Id getopportunityID(){ return opportunityID; }

public void setopportunityID(Id s){
opportunityID = s;
ItineraryBuilderController();
}
public void ItineraryBuilderController() {
listFR = [select Name From Test_Flight_Request__c where Opportunity__c = :opportunityID
and Customer_Approved__c = true
order by Name asc limit 1 ];


}

public string getIB()
{

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://testsite/GetCustomerView?opportunityId='+listFR[0].Opportunity__c+'&flightRequestID='+listFR[0].Id);
req.setMethod('GET');

HttpResponse res = h.send(req);
result = res.getBody();

return result;

}


}

 

Test class

 

@isTest
private class ItineraryBuilderControllerTestCase {

static testMethod void ItineraryBuilderController_TestMetho() {
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();
insert opp;

Test_Flight_Request__c fr = new Test_Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';

insert fr;


}
}

 

please let me know where i am going wrong.

 

thanks in advance


 

 

Best Answer chosen by Admin (Salesforce Developers) 
SRajesh8SRajesh8

Hi

 

Sorry iI forgot the getopportunityID

 

try this

 

Hi,

 

Try this

 

@isTest
private class ItineraryBuilderControllerTestCase {

static testMethod void ItineraryBuilderController_TestMetho() {
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();
insert opp;

Test_Flight_Request__c fr = new Test_Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';
fr.Customer_Approved__c = true;
fr.Flight_Itinerary_Name__c ='test';


insert fr;

System.debug('frrrrrrr'+fr);
Id id1 =opp.id;
ItineraryBuilderController c = new ItineraryBuilderController();
String str =c.getIB();

c.setopportunityID(id1);

Id id2=c.getopportunityID();
}
}

 

and coming to if(listFR != null) check the debug statement which i kept in blue color whether frrrrrrr getting value or not.Because we inserted the Test_Flight_Request__c this should be automatically coveredif not try to make the it as List in test class.

 

Regards,

Rajesh.

All Answers

DoomsdayDoomsday

Hi, try this:

//before insert fr;
fr.Customer_Approved__c = true;


//after insert fr;
ItineraryBuilderController c = new ItineraryBuilderController();
c.setOpportunityID(opp.Id);
String s = c.getIB();
System.assertEquals(c.getOpportunityID(), opp.Id);

 

 

Anup JadhavAnup Jadhav

Haven't you asked this question in a sperate thread in this forum?

 

- A

deepakMdeepakM

hi Doomsday


i did wht you said but  getting error in the below line of  method does'nt exist or incorrect signature

 

 

 


c.setOpportunity(opp.Id);
deepakMdeepakM

i asked but that was little different senario.

DoomsdayDoomsday
c.setOpportunityID(opp.Id);
deepakMdeepakM

yh i knw but even thogh it gives the same

 

"Save error: Method does not exist or incorrect signature: [ItineraryBuilderController].setopportunityID(Id)"

 

c.setOpportunityID(opp.Id);
DoomsdayDoomsday

Are you sure of that saved ItineraryBuilderController class with "public void setopportunityID(Id s)" method?

deepakMdeepakM

ys for you reference i metion the class below:

 

public with sharing class ItineraryBuilderController {
public List<Test_Flight_Request__c> listFR;
public Id opportunityID;
public string result;
public Id getopportunityID(){ return opportunityID; }
public void setopportunityID(Id s){
opportunityID = s;
ItineraryBuilderController();
}
public void ItineraryBuilderController() {
listFR = [select Name From Test_Flight_Request__c where Opportunity__c = :opportunityID
and Customer_Approved__c = true
order by Name asc limit 1 ];
}

public string getIB() {

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://testsite/GetCustomerView?opportunityId='+listFR[0].Opportunity__c+'&flightRequestID='+listFR[0].Id);
req.setMethod('GET');

HttpResponse res = h.send(req);
result = res.getBody();
return result;
}
}

 

thanks

DoomsdayDoomsday

I tested this code on my org and all works fine. But I don't understand why you can't save it

SRajesh8SRajesh8

Hi Deepak,

 

Could u post the updated test class.

 

 

Regards,

Rajesh.

deepakMdeepakM

this is my class below 

 

public with sharing class ItineraryBuilderController {
public List<Test_Flight_Request__c> listFR;
public Id opportunityID;
public string result;
public Id getopportunityID(){ return opportunityID; }
public void setopportunityID(Id s){
opportunityID = s;
ItineraryBuilderController();
}
public void ItineraryBuilderController() {
listFR = [select Name From Test_Flight_Request__c where Opportunity__c = :opportunityID
and Customer_Approved__c = true
order by Name asc limit 1 ];
}

public string getIB() {

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://testsite/GetCustomerView?opportunityId='+listFR[0].Opportunity__c+'&flightRequestID='+listFR[0].Id);
req.setMethod('GET');
HttpResponse res = h.send(req);
result = res.getBody();
return result;
}


}

 

 

test class

 

 

@isTest
private class ItineraryBuilderControllerTestCase {

static testMethod void ItineraryBuilderController_TestMetho() {
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();
insert opp;

Test_Flight_Request__c fr = new Test_Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';
fr.Customer_Approved__c = true;
fr.Flight_Itinerary_Name__c ='test';


insert fr;

ItineraryBuilderController c = new ItineraryBuilderController();
c.getIB();

}
}

deepakMdeepakM

i am just getting 73 % code coverage.

SRajesh8SRajesh8

Hi,

 

Try this

 

@isTest
private class ItineraryBuilderControllerTestCase {

static testMethod void ItineraryBuilderController_TestMetho() {
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();
insert opp;

Test_Flight_Request__c fr = new Test_Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';
fr.Customer_Approved__c = true;
fr.Flight_Itinerary_Name__c ='test';


insert fr;
Id id1 =opp.id;
ItineraryBuilderController c = new ItineraryBuilderController();
String str =c.getIB();

c.setopportunityID(id1);
}
}

 

Regards,

Rajesh.

deepakMdeepakM

SRS8

 

now i am getting 74% coverga

 

the below red lines are not ywt covered

 

 

public with sharing class ItineraryBuilderController {
public List<Test_Flight_Request__c> listFR;
public Id opportunityID;
public string result;
public Id getopportunityID(){ return opportunityID; }
public void setopportunityID(Id s){
opportunityID = s;
ItineraryBuilderController();
}
public void ItineraryBuilderController() {
listFR = [select Name From Test_Flight_Request__c where Opportunity__c = :opportunityID
and Customer_Approved__c = true
order by Name asc limit 1 ];
}

public string getIB() {

if(listFR != null)
{
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://testsite/GetCustomerView?opportunityId='+listFR[0].Opportunity__c+'&flightRequestID='+listFR[0].Id);
req.setMethod('GET');
HttpResponse res = h.send(req);
result = res.getBody();
}
else
{
result ='No data';

}
return result;
}


}

 

thanks

SRajesh8SRajesh8

Hi

 

Sorry iI forgot the getopportunityID

 

try this

 

Hi,

 

Try this

 

@isTest
private class ItineraryBuilderControllerTestCase {

static testMethod void ItineraryBuilderController_TestMetho() {
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();
insert opp;

Test_Flight_Request__c fr = new Test_Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Opportunity__c = opp.Id;
fr.From__c = opp.Origin_City__c;
fr.To__c = opp.Destination_City__c;
fr.Outbound_del__c = opp.Departure_Date__c;
fr.Pax_del__c = opp.Number_of_Passengers__c;
fr.Region_del__c = opp.Destination_Zone__c;
fr.Inbound_del__c = opp.Return_Date__c;
fr.Outbound_Flexibility__c = opp.Flexibility_From__c;
fr.Inbound_Flexibility__c = opp.Flexibility_To__c;
fr.Email_To__c ='anil@swiftsetup.com';
fr.Email_Content__c= 'Test';
fr.Subject__c ='subject test';
fr.Customer_Approved__c = true;
fr.Flight_Itinerary_Name__c ='test';


insert fr;

System.debug('frrrrrrr'+fr);
Id id1 =opp.id;
ItineraryBuilderController c = new ItineraryBuilderController();
String str =c.getIB();

c.setopportunityID(id1);

Id id2=c.getopportunityID();
}
}

 

and coming to if(listFR != null) check the debug statement which i kept in blue color whether frrrrrrr getting value or not.Because we inserted the Test_Flight_Request__c this should be automatically coveredif not try to make the it as List in test class.

 

Regards,

Rajesh.

This was selected as the best answer
deepakMdeepakM

thanks rajesh 

 

it work

DoomsdayDoomsday

 

//before insert fr;
fr.Customer_Approved__c = true;


//after insert fr;
ItineraryBuilderController c = new ItineraryBuilderController();
c.setOpportunityID(opp.Id);
String s = c.getIB();
System.assertEquals(c.getOpportunityID(), opp.Id);

"Id id1 =opp.id;
ItineraryBuilderController c = new ItineraryBuilderController();
String str =c.getIB();

c.setopportunityID(id1);

Id id2=c.getopportunityID();"

P.S: Find 10 differences..