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
Anil DuttAnil Dutt 

Code Coverage Required

Hi, Please help on writing test case to get 100% code coverage . Lines in Red not covered

 

Here is the Apex 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;
            }
            if (fr.To__c == null)
            {
                fr.To__c = listOpp[0].Destination_City__c;
            }
            if (fr.Outbound_del__c == null)
            {
                fr.Outbound_del__c = listOpp[0].Departure_Date__c;
            }
            if (fr.Pax_del__c == null)
            {
                fr.Pax_del__c = listOpp[0].Number_of_Passengers__c;
            }
            if (fr.Region_del__c == null)
            {
                fr.Region_del__c = listOpp[0].Destination_Zone__c;
            }
            if (fr.Inbound_del__c == null)
            {
                fr.Inbound_del__c = listOpp[0].Return_Date__c;
            }
            if (fr.Outbound_Flexibility__c == null)
            {
                fr.Outbound_Flexibility__c = listOpp[0].Flexibility_From__c;
            }
            if (fr.Inbound_Flexibility__c == null)
            {
                fr.Inbound_Flexibility__c = listOpp[0].Flexibility_To__c;
            }
        }
    }
    
    public PageReference Save()
    {
        try
        {
            if(fr.Id ==null)
                insert fr;
            else
                update fr;
        }
        catch(DmlException ex)
          {
                ApexPages.addMessages(ex);  
        }
        PageReference pr = new PageReference('/'+  Id);
        pr.setRedirect(true);
        return pr;
    }
    
    public PageReference SaveAndNew()
    {
        try
        {
            if(fr.Id ==null)
                insert fr;
            else
                update fr;
        }
        catch(DmlException ex)
        {
            ApexPages.addMessages(ex);  
        }
        PageReference pr = new PageReference('/' + Id);
        pr.setRedirect(true);
        return pr;
    }
   
     public void SendItineryEmail()
     {
          Http h = new Http();
         HttpRequest req = new HttpRequest();
        req.setEndpoint('http://itinerary.swiftsetup.com/ItineraryBuilder/GetCustomerView?opportunityId='+fr.Opportunity__c+'&flightRequestID='+fr.Id);
        req.setMethod('GET');
           
        HttpResponse res = h.send(req);
        result = res.getBody();
        String[] toaddress = new String[]{};
        toaddress.add('anil@swiftsetup.com');
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toaddress);
        mail.setSubject('Flight Stat');
        mail.setHtmlBody(result);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
     }
    
     public PageReference CloneFlightRequest()
    {
        try
        {
            Test_Flight_Request__c clFR = new Test_Flight_Request__c();
            Test_Flight_Request__c oldFR = [SELECT From__c, To__c,Outbound_del__c,Pax_del__c,Region_del__c,        
                    Inbound_del__c,Outbound_Flexibility__c,Inbound_Flexibility__c,Opportunity__c,
                    Budget__c,Publish_Fare__c
                 FROM Test_Flight_Request__c WHERE Id = :fr.Id];

            clFR.From__c = oldFR.From__c;
            clFR.To__c = oldFR.To__c;
            clFR.Outbound_del__c = oldFR.Outbound_del__c;
            clFR.Pax_del__c = oldFR.Pax_del__c;
            clFR.Region_del__c = oldFR.Region_del__c;
            clFR.Inbound_del__c = oldFR.Inbound_del__c;
            clFR.Outbound_Flexibility__c = oldFR.Outbound_Flexibility__c;
            clFR.Inbound_Flexibility__c = oldFR.Inbound_Flexibility__c;
            clFR.Opportunity__c = oldFR.Opportunity__c;
            clFR.Budget__c = oldFR.Budget__c;
            clFR.Publish_Fare__c = oldFR.Publish_Fare__c;    
            insert clFR;
            
        }
        catch(DmlException ex)
        {
            ApexPages.addMessages(ex);  
        }
        PageReference pr = new PageReference('/' + Id);
        pr.setRedirect(true);
        return pr;
    }
}

jha.pk5@cloud.comjha.pk5@cloud.com

Can you post your test class, so that someone  will help you to increase 100% code coverage.

Anil DuttAnil Dutt

Test Case is

 

@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';
            
        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;
            }
            if (fr.To__c == null)
            {
                fr.To__c = listOpp[0].Destination_City__c;
            }
            if (fr.Outbound_del__c == null)
            {
                fr.Outbound_del__c = listOpp[0].Departure_Date__c;
            }
            if (fr.Pax_del__c == null)
            {
                fr.Pax_del__c = listOpp[0].Number_of_Passengers__c;
                
                passengers  = listOpp[0].Number_of_Passengers__c.intvalue();
            }
            if (fr.Region_del__c == null)
            {
                fr.Region_del__c = listOpp[0].Destination_Zone__c;
            }
            if (fr.Inbound_del__c == null)
            {
                fr.Inbound_del__c = listOpp[0].Return_Date__c;
            }
            if (fr.Outbound_Flexibility__c == null)
            {
                fr.Outbound_Flexibility__c = listOpp[0].Flexibility_From__c;
            }
            if (fr.Inbound_Flexibility__c == null)
            {
                fr.Inbound_Flexibility__c = listOpp[0].Flexibility_To__c;
            }
        }
        
        insert fr;
        update fr;
        
        
        
        try
        {
            ApexPages.StandardController sc = new ApexPages.StandardController(fr);
            TestFlighRequestPage sc1 = new TestFlighRequestPage(sc);
            sc1.Save();
            sc1.SaveAndNew();
            sc1.SendItineryEmail();
            sc1.CloneFlightRequest();
            
            Http h = new Http();
             HttpRequest req = new HttpRequest();
            req.setEndpoint('http://itinerary.swiftsetup.com/ItineraryBuilder/GetCustomerView?opportunityId='+fr.Opportunity__c+'&flightRequestID='+fr.Id);
            req.setMethod('GET');
               
            HttpResponse res = h.send(req);
            String result = res.getBody();
            String[] toaddress = new String[]{};
            toaddress.add('anil@swiftsetup.com');
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toaddress);
            mail.setSubject('Flight Stat');
            mail.setHtmlBody(result);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           
               Test_Flight_Request__c clFR = new Test_Flight_Request__c();
            Test_Flight_Request__c oldFR = [SELECT From__c, To__c,Outbound_del__c,Pax_del__c,Region_del__c,        
                        Inbound_del__c,Outbound_Flexibility__c,Inbound_Flexibility__c,Opportunity__c,
                        Budget__c,Publish_Fare__c
                     FROM Test_Flight_Request__c WHERE Id = :fr.Id];
    
            clFR.From__c = oldFR.From__c;
            clFR.To__c = oldFR.To__c;
            clFR.Outbound_del__c = oldFR.Outbound_del__c;
            clFR.Pax_del__c = oldFR.Pax_del__c;
            clFR.Region_del__c = oldFR.Region_del__c;
            clFR.Inbound_del__c = oldFR.Inbound_del__c;
            clFR.Outbound_Flexibility__c = oldFR.Outbound_Flexibility__c;
            clFR.Inbound_Flexibility__c = oldFR.Inbound_Flexibility__c;
            clFR.Opportunity__c = oldFR.Opportunity__c;
            clFR.Budget__c = oldFR.Budget__c;
            clFR.Publish_Fare__c = oldFR.Publish_Fare__c;
            insert clFR;
        }
        catch(System.CalloutException e)
        {
            message = e.getMessage();
        }
    }
}

jha.pk5@cloud.comjha.pk5@cloud.com

some one  is working on your issue.Once he wil do i will post the solution