+ Start a Discussion
kumar 197kumar 197 

Can Anyone help me write Test Class for this???

<apex:page controller="FlightController">
    <apex:pageBlock title="Select Flights">
      <apex:form >
        <apex:pageBlockSection >
       <apex:pageBlockSectionItem >
       <apex:outputText value="Flights"/>
        <apex:selectList size="1" value="{! fosId }">
        <apex:selectOptions value="{! fosList }"/>
        <apex:actionSupport event="onchange" reRender="FL01"/>
         </apex:selectList>
        </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
         </apex:form>   
         </apex:pageBlock>
          <apex:pageBlock id="FL01">
        <apex:detail subject="{!fosId}" relatedlist="True"/>
       <apex:relatedList list="Booking__c"/>      
    </apex:pageBlock>
</apex:page>
--------------------------------------------
public class FlightController {

    public List<SelectOption> flights = new List<SelectOption>();
    public Id fosId {get;set;}
    
    public List<SelectOption> getfosList(){
        for(Flight__c fos : [SELECT Id, Name,(SELECT Name,Id FROM Bookings__r  ORDER BY BoardingDate__c DESC) FROM Flight__c]){
            flights.add(new SelectOption(fos.Id, fos.Name));
        }
        return flights;
    }
}
Best Answer chosen by kumar 197
kumar 197kumar 197
Hi  CharuDutt i made few Changes and now i am getting 100% coverage.

@isTest
private class FlightControllerTest {
    @isTest static void myTest() {
        Flight__c f = new Flight__c();
        f.Name = 'Test Flight';
        insert f;
        Customer__c c = new Customer__c();
        c.Name = 'Testc';
        insert c;
        Booking__c b =new Booking__c();
        //  b.Name='Test Bookings';
        b.Flight__c = f.Id;
        b.Customer__c= c.Id;
        Insert b;
        pagereference testPage=page.FlightController;
        FlightController fc = new FlightController();
        fc.fosId = f.Id;
        fc.getfosList();
        
    }
}

All Answers

CharuDuttCharuDutt
Hii Kumar
Try Below Test Class
@isTest 
 public Class FlightControllerTest { 
 @isTest
 public Static Void unitTest(){
		Flight__c f = new Flight__c();
	f.Name = 'Test flight';
	Insert f;
	
	Bookings__c B = new Bookings__c();
	b.name = 'test Booking';
	b.Flight__c = f.Id;
	Insert b;
	
     FlightController fc = new FlightController();
     fc.fosId = f.Id;
     fc.getfosList();
	
 }
 }
Please Mark It As Best Answer If It Helps
Thank You!
kumar 197kumar 197
Hi charuDutt
I tryed your code but but getting error : Field not writeable:Booking__c.Name    .So i allow the reparenting on both master Flight__c and Customer__c but its not working yet. Here Booking is the Junction between flight and customer.
kumar 197kumar 197
Hi  CharuDutt i made few Changes and now i am getting 100% coverage.

@isTest
private class FlightControllerTest {
    @isTest static void myTest() {
        Flight__c f = new Flight__c();
        f.Name = 'Test Flight';
        insert f;
        Customer__c c = new Customer__c();
        c.Name = 'Testc';
        insert c;
        Booking__c b =new Booking__c();
        //  b.Name='Test Bookings';
        b.Flight__c = f.Id;
        b.Customer__c= c.Id;
        Insert b;
        pagereference testPage=page.FlightController;
        FlightController fc = new FlightController();
        fc.fosId = f.Id;
        fc.getfosList();
        
    }
}
This was selected as the best answer