You need to sign in to do that
Don't have an account?
Sylvio Avilla
Help to create a Test
Hello,
I'm having trouble to build a test for the following code
Could anyone help me?
Thanks
Sylvio
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
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
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.
}
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.
I've insert the both codes but I’m receiving an error message:
Error:
Test 1 (R Z Khan) Test 2 (Ankit 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.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
Got it!! But I used a diferent solution...
Thank so much for your and R Z Khan help!