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
syed akramsyed akram 

How to write test class for below code?

public without sharing class UpdateList {
    list<order> selectorder;
    String quoteId {set; get;}
    String selectedOrderId {set; get;}
    public List<Order> selectedOrders {set; get;}
    public Boolean orderUpdated {set; get;}
    
    public UpdateList(){
        quoteId = ApexPages.currentPage().getParameters().get('quoteID');
        orderUpdated = false;
    }
    
    public PageReference selectOrder(){
        selectedOrderId = ApexPages.currentPage().getParameters().get('ordersRadio');
        selectedOrders = [select Id, OrderNumber, Status from Order where id=:selectedOrderId];
        return null;
    }
    
    public list<Order> getorderslist()
    {
        List<order> allords=[select Id,Ordernumber,Status from Order where quoteId=:quoteId];
        return allords;
    }
    
    //call this method to update order, and copy all quote line items from quote
    public PageReference updateOrder(){
        
        //get selected orderId from the list   
        selectedOrderId = ApexPages.currentPage().getParameters().get('ordersRadio');

        //get quote line items using the quoteId
        List<OrderItem> orderLines = new List<OrderItem>(); 
        for(QuoteLineItem qLine : [SELECT Id, QuoteId, PricebookEntryId, Quantity, UnitPrice, Discount, Description, ServiceDate, 
                                   Product2Id, SortOrder, ListPrice, Subtotal, TotalPrice FROM QuoteLineItem where QuoteId=:quoteId]){
            OrderItem oLine         = new OrderItem();
            oLine.OrderId           = selectedOrderId;
            oLine.PricebookEntryId  = qLine.PricebookEntryId;
            oLine.Quantity          = qLine.Quantity;
            oLine.UnitPrice         = qLine.UnitPrice;
            oLine.Description       = qLine.Description;
            oLine.ServiceDate       = qLine.ServiceDate;
            orderLines.add(oLine);
        }      
        insert orderLines;    
        orderUpdated = true;
         ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record updated Successfully.Thank you!'));
        return null;        
    }    
}
Bertrand PolusBertrand Polus
I suppose you will probably have to change the following code:
quoteId = ApexPages.currentPage().getParameters().get('quoteID');
with something like this
if(!test.isrunningtest()) {
    quoteId = ApexPages.currentPage().getParameters().get('quoteID');
} else {
   quoteId = [Select id from xxxQuoteTable LIMIT 1];
}
(Same thing for other location where you are getting some parameters from the URL)

After that, you will just have to create a normal test class, creating some data in your table and calling the methods to test.

Hoping this will help you...