You need to sign in to do that
Don't have an account?
Need Test class for this apex class
hi every one i have bloew class ,i need a test class in order to deploy this as i create the test class but it cover only 74% i dont know which line is not coverd
Class
public with sharing class TestFlighRequestPage {
Test_Flight_Request__c fr;
public List<Opportunity> listOpp;
public String Id {get; set;}
private string result;
public TestFlighRequestPage(ApexPages.StandardController stdController)
{
fr = (Test_Flight_Request__c)stdController.getRecord();
if(fr.Id == null)
Id = ApexPages.currentPage().getParameters().get('opp');
else
Id = fr.Opportunity__c;
listOpp = [SELECT id ,Name, Departure_Date__c,Number_of_Passengers__c,Destination_Zone__c,Return_Date__c,
Origin_City__c,Destination_City__c,Flexibility_From__c,Flexibility_To__c
FROM Opportunity WHERE Id = :Id];
if(listOpp.size() >0)
{
if(fr.Opportunity__c == null)
{
fr.Opportunity__c = listOpp[0].Id;
}
if (fr.From__c == null)
{
fr.From__c = listOpp[0].Origin_City__c;
}
}
}
public void SendItineryEmail()
{
Test_Flight_Request__c FRdata = [SELECT Email_To__c, Subject__c,Email_Content__c
FROM Test_Flight_Request__c WHERE Id = :fr.Id];
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(' ');
req.setMethod('GET');
HttpResponse res = h.send(req);
result = res.getBody();
String[] toaddress = new String[]{};
toaddress.add(FRdata.Subject__c);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toaddress);
mail.setSubject(FRdata.Subject__c);
string Emailbodydata = FRdata.Email_Content__c +'<br>'+ result;
mail.setHtmlBody(Emailbodydata);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
TestClass
@isTest
private class TestFlighRequestPageTestCase {
static testMethod void myUnitTest() {
String message= '';
boolean flag= true;
String Id='';
double passengers = 0;
Contact con = new Contact();
con.FirstName = 'Anil';
con.LastName = 'Dutt';
con.Email = 'anil@swiftsetup.com';
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.StageName = 'Ticketing';
opp.p1_First_Name__c ='';
opp.p1_Last_Name__c ='';
opp.p1_Gender__c = '';
opp.p1_First_Name__c ='';
opp.p1_Last_Name__c ='';
opp.p1_Gender__c = '';
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;
string oppID = opp.Id;
Test_Flight_Request__c fr = new Test_Flight_Request__c();
fr.Budget__c = '23';
fr.Publish_Fare__c = '35';
fr.Email_To__c ='anil@swiftsetup.com';
fr.Subject__c ='Test Mail';
fr.Email_Content__c ='Email Content';
List<Opportunity> listOpp = [SELECT id ,Name, Departure_Date__c,Number_of_Passengers__c,Destination_Zone__c,Return_Date__c,
Origin_City__c,Destination_City__c,Flexibility_From__c,Flexibility_To__c
FROM Opportunity WHERE Id = :opp.Id];
if(listOpp.size() > 0 )
{
if(fr.Opportunity__c == null)
{
fr.Opportunity__c = listOpp[0].Id;
}
if (fr.From__c == null)
{
fr.From__c = listOpp[0].Origin_City__c;
}
}
insert fr;
update fr;
Test_Flight_Request__c FRdata = [SELECT Email_To__c, Subject__c,Email_Content__c
FROM Test_Flight_Request__c WHERE Id = :fr.Id];
FRdata.Email_To__c = fr.Email_To__c;
FRdata.Subject__c = fr.Subject__c;
FRdata.Email_Content__c = fr.Email_Content__c;
try
{
ApexPages.StandardController sc = new ApexPages.StandardController(fr);
TestFlighRequestPage sc1 = new TestFlighRequestPage(sc);
sc1.SendItineryEmail();
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(' ');
req.setMethod('GET');
HttpResponse res = h.send(req);
String result = res.getBody();
String[] toaddress = new String[]{};
toaddress.add(FRdata.Email_To__c);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toaddress);
mail.setSubject(FRdata.Subject__c);
string Emailbodydata = FRdata.Email_Content__c +'<br>'+ result;
mail.setHtmlBody(Emailbodydata);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
catch(System.CalloutException e)
{
message = e.getMessage();
}
}
}
You can not use HTTP for testing. Code coverage will stop when reaching HTTP or send (i quess).
You need to simulate the response data for testing.
but i already depoly the test class with http is mention in it
i lttile change my class after that and also make that change in test class too .but unable to deploy that time.
Run test then you'll see which line are not coveraged. So you can fix it.
issue is that test class displays 0 lines not tested 100% covered
but it displays that in actual class 21 lines are not covered.
when i deploy the old code it successfully done but with new changes as i mention it cnt.
note: changes shown in bold