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
Sylvio AvillaSylvio Avilla 

Help to create a Test

Hello,

I'm having trouble to build a test for the following code
 
public class AtualizaItemDoOrcamento {

    private List<Item_da_Compra__c> comp;
    private Id quoteId;

    public AtualizaItemDoOrcamento(ApexPages.StandardSetController c) {
        c.setPageSize(10);
        this.quoteId = ApexPages.currentPage().getParameters().get('id');
    }
     
   
   public List<Item_da_Compra__c> getcomp() {
       
        comp = [Select Id, Name, Quantidade_Orcada_Fornecedor_1__c, Status_do_Or_amento__c,Valor_Unitario_Fornecedor_1__c
                ,Fornecedor__c,Item__c, Marca__c ,C_digo_do_Produto__c from Item_da_Compra__c where Or_amento__c = :this.quoteId];
        return comp;
    }

    public pageReference save() {
        update comp;
        PageReference ret = new PageReference(ApexPages.currentPage().getParameters().get('retURL'));
            return ret.setRedirect(true);
    }

    public PageReference cancel() {
        PageReference ret = new PageReference(ApexPages.currentPage().getParameters().get('retURL'));
        return ret.setRedirect(true);
    }

}

Could anyone help me?

Thanks

Sylvio
Best Answer chosen by Sylvio Avilla
Ankit SehgalAnkit Sehgal
@isTest
public class testAtualizaItemDoOrcamento
{
   static testMethod void test()
   {
        PageReference pageRef = Page.<your visualforce page name>;
        Test.setCurrentPage(pageRef);
         Item_da_Compra__c item = new Item_da_Compra__c(<insert necessary values here>);
         insert item;
         ApexPages.currentPage().getParameters().put('id', item.id);
         ApexPages.currentPage().getParameters().put('retURL',<insert url here>);
         ApexPages.StandardController sc = new ApexPages.StandardController(<sObject name here>);
         AtualizaItemDoOrcamento testObj=new AtualizaItemDoOrcamento(sc);
         Test.startTest();
           testObj.getcomp();
           testObj.save();
           testObj.cancle();
         Test.stopTest();
   }
}

you might get error whiile inserting Item_da_compra__c object if all the necessary field values are not provided i.e if there is a lookup field in this object then that object also needs to be inserted in the test class before item_da_comp__c can be inserted.

All Answers

R Z KhanR Z Khan
In order to test an extension for the standardSetController, you first need to create a record that htis controller is used for, then create a set controller that oyu are going to pass into your contructor.
Also you would need to set a page for your test and set all fo the parameters used. 

@isTest static void testCancel(){
      PageReference pageRef = Page.YourPage;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('retURL','/myPage');
    //create myRecord
     ApexPages.StandardController sc = new ApexPages.StandardController(myRecord);
     AtualizaItemDoOrcamento ctrl = new AtualizaItemDoOrcamento();
     String retURL = ctrl.save().getUrl();
      System.assertEquals(retURL, '/myPage');
}
 @isTest static void testGetComp(){
       PageReference pageRef = Page.YourPage;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('retURL','/myPage');

      //create myRecord
     Item_da_Compra__c item = new Item_da_Compra__c(Name='test', Or_amento__c = myRecord.Id);
     insert item;

ApexPages.StandardController sc = new ApexPages.StandardController(myRecord);
     AtualizaItemDoOrcamento ctrl = new AtualizaItemDoOrcamento();
List<Item_da_Compra__c> items = ctrl.getcomp();
//add your assertion on number of records and Id of the record returned.
}

@isTest static void testSave(){
      same as testGetComp.
modified the returned List<Item_da_Compra__c>. then call the save method. Make sure that when you call the getcomp() method, returned lsit has the values you modified.
   
}
Ankit SehgalAnkit Sehgal
@isTest
public class testAtualizaItemDoOrcamento
{
   static testMethod void test()
   {
        PageReference pageRef = Page.<your visualforce page name>;
        Test.setCurrentPage(pageRef);
         Item_da_Compra__c item = new Item_da_Compra__c(<insert necessary values here>);
         insert item;
         ApexPages.currentPage().getParameters().put('id', item.id);
         ApexPages.currentPage().getParameters().put('retURL',<insert url here>);
         ApexPages.StandardController sc = new ApexPages.StandardController(<sObject name here>);
         AtualizaItemDoOrcamento testObj=new AtualizaItemDoOrcamento(sc);
         Test.startTest();
           testObj.getcomp();
           testObj.save();
           testObj.cancle();
         Test.stopTest();
   }
}

you might get error whiile inserting Item_da_compra__c object if all the necessary field values are not provided i.e if there is a lookup field in this object then that object also needs to be inserted in the test class before item_da_comp__c can be inserted.
This was selected as the best answer
Sylvio AvillaSylvio Avilla
Thank you R Z Khan and Ankit Sehgal for the quick reply...

I've insert the both codes but I’m receiving an error message:

Error:
 
Constructor not defined: [AtualizaItemDoOrcamento].&lt;Constructor&gt;()

Test 1 (R Z Khan)
@isTest
public class testAtualizaItemDoOrcamento
{    
   @isTest static void testCancel(){
      PageReference pageRef = Page.AtualizaItemOrcamento;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('retURL','/myPage');
    //create myRecord
     date mydate = date.parse('01/01/2015');
     Orcamento__c orcamento = new Orcamento__c (Data_do_Orcamento__c = mydate );
     insert orcamento;
     Item_da_Compra__c item = new Item_da_Compra__c(Or_amento__c = orcamento.Id);
     insert item;
     ApexPages.StandardController sc = new ApexPages.StandardController(orcamento);
     AtualizaItemDoOrcamento ctrl = new AtualizaItemDoOrcamento();
     String retURL = ctrl.save().getUrl();
      System.assertEquals(retURL, '/myPage');
}
 @isTest static void testGetComp(){
       PageReference pageRef = Page.AtualizaItemOrcamento;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('retURL','/myPage');

      //create myRecord
     date mydate = date.parse('01/01/2015');
     Orcamento__c orcamento = new Orcamento__c (Data_do_Orcamento__c = mydate );
     insert orcamento;
     Item_da_Compra__c item = new Item_da_Compra__c(Or_amento__c = orcamento.Id);
     insert item;

ApexPages.StandardController sc = new ApexPages.StandardController(orcamento);
     AtualizaItemDoOrcamento ctrl = new AtualizaItemDoOrcamento();
List<Item_da_Compra__c> items = ctrl.getcomp();
//add your assertion on number of records and Id of the record returned.
}

@isTest static void testSave(){
       PageReference pageRef = Page.AtualizaItemOrcamento;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('retURL','/myPage');

      //create myRecord
     date mydate = date.parse('01/01/2015');
     Orcamento__c orcamento = new Orcamento__c (Data_do_Orcamento__c = mydate );
     insert orcamento;
     Item_da_Compra__c item = new Item_da_Compra__c(Or_amento__c = orcamento.Id);
     insert item;
}
}
Test 2 (Ankit Sehgal)
@isTest
public class testAtualizaItemDoOrcamento
{
   static testMethod void test()
   {
        PageReference pageRef = Page.AtualizaItemOrcamento;
        Test.setCurrentPage(pageRef);
        date mydate = date.parse('01/01/2015');
        Orcamento__c orcamento = new Orcamento__c (Data_do_Orcamento__c = mydate );
        insert orcamento;
        Item_da_Compra__c item = new Item_da_Compra__c (Or_amento__c = orcamento.ID);
        insert item;

         ApexPages.currentPage().getParameters().put('id', item.id);
         ApexPages.currentPage().getParameters().put('retURL','/myPage');
       
         ApexPages.StandardController sc = new ApexPages.StandardController(item);
         AtualizaItemDoOrcamento testObj=new AtualizaItemDoOrcamento(sc);
         Test.startTest();
           testObj.getcomp();
           testObj.save();
           testObj.cancel();
         Test.stopTest();
   }
}

 
Ankit SehgalAnkit Sehgal
Try this:

@isTest
public class testAtualizaItemDoOrcamento
{
   static testMethod void test()
   {
        PageReference pageRef = Page.<your visualforce page name>;
        Test.setCurrentPage(pageRef);
         Item_da_Compra__c item = new Item_da_Compra__c(<insert necessary values here>);
         insert item;
         ApexPages.currentPage().getParameters().put('id', item.id);
         ApexPages.currentPage().getParameters().put('retURL',<insert url here>);
         ApexPages.StandardSetController sc = new ApexPages.StandardSetController(<sObject name here>);
         AtualizaItemDoOrcamento testObj=new AtualizaItemDoOrcamento(sc);
         Test.startTest();
           testObj.getcomp();
           testObj.save();
           testObj.cancle();
         Test.stopTest();
   }
}

The error was probably because we are using StandardController in test class instead of StandardSetController
Sylvio AvillaSylvio Avilla
Hello Ankit Sehgal,

Got it!! But I used a diferent solution... 
 
@isTest
public class TesteAtualizaItemDoOrcamento2 {
    static testMethod void test()
   {
        PageReference pageRef = Page.AtualizaItemOrcamento;
        Test.setCurrentPage(pageRef);
        date mydate = date.parse('01/01/2015');
        Orcamento__c orcamento = new Orcamento__c (Data_do_Orcamento__c = mydate );
        insert orcamento;
        Item_da_Compra__c item = new Item_da_Compra__c (Or_amento__c = orcamento.ID);
        insert item;

         ApexPages.currentPage().getParameters().put('id', item.id);
         ApexPages.currentPage().getParameters().put('retURL','/myPage');
       
      List<Item_da_Compra__c> opps = new List<Item_da_Compra__c>();
      opps.add( [ Select Id From Item_da_Compra__c Where Or_amento__c = :orcamento.Id ] );
      

         ApexPages.StandardController sc = new ApexPages.StandardController(item);
         AtualizaItemDoOrcamento testObj=new AtualizaItemDoOrcamento(new ApexPages.StandardSetController( opps ));
         Test.startTest();
           testObj.getcomp();
           testObj.save();
           testObj.cancel();
         Test.stopTest();
   }
}

Thank so much for your and R Z Khan help!