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 

Help in writing Test class for particular method

here is the Controller 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;                     
    }    
}

here is the test class i have written till now i have written for one method and getting 25% code coverage
@istest
private class TestUpdateList{
    
    Private static Account a;
    Private static Contact c;
    Private static Opportunity opp;
    Private static Quote q;
    private static Order o;
   Static {    
    // Creating the data for Account,Contact,Opportunity,Quote and Order.
    
           a=new Account(Name='test1',Phone='9458383336');   // creating account with only mandatory Fields
            insert a;
           c = new Contact(LastName='Test',AccountId=a.Id);  // creating contact with only mandatory Fields
            insert c;
            
    // creating opportunity with only mandatory Fields
           opp = new Opportunity(Name='Test Opp',AccountId=a.Id,StageName='closedwon',CloseDate=Date.Today());
            insert opp;
            
    // Creating Quote with only mandatory Fields
           q=new Quote(Name='testquote',opportunityId=opp.Id);
            insert q;
           o=new Order(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today());
            insert o;                    
   }  
   // retreiving the order record and  working fine 25% code coverage with this method.
      Static TestMethod void Testgetorderslist(){
      Test.StartTest();
      PageReference pageRef=page.PrevOrder;
      Test.SetCurrentPageReference(pageRef);
      system.currentPageReference().getParameters().put('Id', o.id);
      string ol = system.currentPageReference().getParameters().get('Id');
      system.debug('o.Id = '+ ol);
      UpdateList ul=new UpdateList();
      List<order> orderList = ul.getorderslist();
      system.debug('test result ='+ orderList);
      system.assertEquals(orderList.size(),1);
      Test.StopTest();
      }
    
    //
    Static TestMethod void TestselectOrder(){
    PageReference pageRef=page.PrevOrder;
    Test.SetCurrentPageReference(pageRef);
    Test.StartTest();
    
   // ApexPages.StandardController sc = new ApexPages.standardController(o);
   // UpdateList controller=new UpdateList(sc);
   // controller.SelectOrder();
    
    
    }
    
    Static TestMethod void TestUpdateOrder(){
    }
}

Please help me to write the test for TestSelectOrder() ?
Best Answer chosen by syed akram
syed akramsyed akram

 instead of get i need to write put its working
ApexPages.currentPage().getParameters().put('ordersRadio',o.Id);  

All Answers

ManojjenaManojjena
Hi  syed ,
try to replace
  system.currentPageReference().getParameters().put('Id', o.id); with below
ApexPages.currentPage().getParameters().get('ordersRadio',o.Id);

let me know  if it helps.
Thanks
Manoj
 
syed akramsyed akram
i am getting error as
Error: Compile Error: Method does not exist or incorrect signature: [Map<String,String>].get(String, Id) at line 47 column 45
syed akramsyed akram

 instead of get i need to write put its working
ApexPages.currentPage().getParameters().put('ordersRadio',o.Id);  
This was selected as the best answer
syed akramsyed akram
@istest
private class TestUpdateList{
    
    Private static Account a;
    Private static Contact c;
    Private static Opportunity opp;
    Private static Quote q;
    Private static QuoteLineItem Qli;
    private static Order o;
   Static {    
    // Creating the data for Account,Contact,Opportunity,Quote and Order.
    
           a=new Account(Name='test1',Phone='9458383336');   // creating account with only mandatory Fields
            insert a;
           c = new Contact(LastName='Test',AccountId=a.Id);  // creating contact with only mandatory Fields
            insert c;
            
    // creating opportunity with only mandatory Fields
           opp = new Opportunity(Name='Test Opp',AccountId=a.Id,StageName='closedwon',CloseDate=Date.Today());
            insert opp;
            
    // Creating Quote with only mandatory Fields
          q=new Quote(Name='testquote',opportunityId=opp.Id);
            insert q;
    // Creating Quote Line Items
          qli=new QuoteLineItem(QuoteId=q.Id,Quantity=12,UnitPrice=1500,Discount=0,Description='nothing',ServiceDate=system.today());
            insert qli;
    // Creating Order with mandatory Fields 
           o=new Order(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today());
            insert o;                    
   }  
   // retreiving the order record and  working fine 25% code coverage with this method.
      Static TestMethod void Testgetorderslist(){
      Test.StartTest();
      PageReference pageRef=page.PrevOrder;
      Test.SetCurrentPageReference(pageRef);
      system.currentPageReference().getParameters().put('Id', o.id);
      string ol = system.currentPageReference().getParameters().get('Id');
      system.debug('o.Id = '+ ol);
      UpdateList ul=new UpdateList();
      List<order> orderList = ul.getorderslist();
      system.debug('test result ='+ orderList);
      system.assertEquals(orderList.size(),1);
      Test.StopTest();
      }
    
    //after this method i am getting 46% code
    Static TestMethod void TestselectOrder(){
    Test.StartTest();
    PageReference pageRef=page.PrevOrder;
    Test.SetCurrentPageReference(pageRef);
    ApexPages.currentPage().getParameters().put('ordersRadio',o.Id);   
    ApexPages.StandardController sc = new ApexPages.standardController(o);              
    UpdateList controller=new UpdateList();   
       controller.SelectOrder();  
       controller.UpdateOrder();
       Test.StopTest();    
    }
    
    //Static TestMethod void TestUpdateOrder(){
    //Test.StartTest();
    //PageReference pageRef=page.PrevOrder;
   // Test.SetCurrentPageReference(pageRef);
    
    //}
}


can you please help me in this    // Creating Quote Line Items
          qli=new QuoteLineItem(QuoteId=q.Id,Quantity=12,UnitPrice=1500,Discount=0,Description='nothing',ServiceDate=system.today());
            insert qli;
i need the remaining field with some dummy value like listprice,subtotal,totalprice.
if i am writing i am getting error as writable field