You need to sign in to do that
Don't have an account?
Kiran G
Test Class for custom extension
Hi,
I have a custom object called procurement with and extension which is used by a visual force page. It works fine but now i need to move it to production and after trying out a lot I am not able to write a test class that works. Please help me write a test for the below extension class . I have also pasted the test class that I tried but doesn't work. Not sure if I am going in the right direction.
Extension class
public with sharing class ProcurementExtension {
private ApexPages.StandardController controller;
private Set<String> ProcurementFields = new Set<String>();
public ProcurementExtension (ApexPages.StandardController controller) {
this.controller = controller;
Map<String, Schema.SobjectField> fields =
Schema.SobjectType.Procurement__c.fields.getMap();
for (String s : fields.keySet()) {
// Only include accessible fields
if (fields.get(s).getDescribe().isAccessible() &&
fields.get(s).getDescribe().isCustom()) {
ProcurementFields.add(s);
}
}
}
public List<String> availableFields {
get {
controller.reset();
controller.addFields(new List<String>(ProcurementFields));
return new List<String>(ProcurementFields);
}
}
}
Test Class I have so far
@isTest
private class testProcurementExtension
{static testMethod void ProcurementExtension(){
Procurement__c objProcurement = new Procurement__c();
objProcurement.Name = 'Test';
objProcurement.Account__c = 'Test Account';
insert objProcurement;
// Start the test, this changes governor limit context to
// that of trigger rather than test.
test.startTest();
ApexPages.currentPage().getParameters().put('lc',objProcurement.id);
test.stopTest();
}
}
I have a custom object called procurement with and extension which is used by a visual force page. It works fine but now i need to move it to production and after trying out a lot I am not able to write a test class that works. Please help me write a test for the below extension class . I have also pasted the test class that I tried but doesn't work. Not sure if I am going in the right direction.
Extension class
public with sharing class ProcurementExtension {
private ApexPages.StandardController controller;
private Set<String> ProcurementFields = new Set<String>();
public ProcurementExtension (ApexPages.StandardController controller) {
this.controller = controller;
Map<String, Schema.SobjectField> fields =
Schema.SobjectType.Procurement__c.fields.getMap();
for (String s : fields.keySet()) {
// Only include accessible fields
if (fields.get(s).getDescribe().isAccessible() &&
fields.get(s).getDescribe().isCustom()) {
ProcurementFields.add(s);
}
}
}
public List<String> availableFields {
get {
controller.reset();
controller.addFields(new List<String>(ProcurementFields));
return new List<String>(ProcurementFields);
}
}
}
Test Class I have so far
@isTest
private class testProcurementExtension
{static testMethod void ProcurementExtension(){
Procurement__c objProcurement = new Procurement__c();
objProcurement.Name = 'Test';
objProcurement.Account__c = 'Test Account';
insert objProcurement;
// Start the test, this changes governor limit context to
// that of trigger rather than test.
test.startTest();
ApexPages.currentPage().getParameters().put('lc',objProcurement.id);
test.stopTest();
}
}
You can start test class like belwo . For few tip and tricks you can check belwo link it wil help !!
http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html
Let me know if it helps !!
Thanks
Manoj
But the test is still failing when I run it directly. I do not have too much knowledge on coding. I just want to move this to production. Should I be calling out more fields before the "Test.startTest();"? Please note all other fields apart from "Accounts" on Procurement are not writable. I have removed the Procurement.Name as well since it is an auto number. Please let me know where I am going wrong.
You need to add mandatory fields in test records.
I think account is a look up field in Procuremanet .Check with belwo code and let me know the error you are getting .
I this I will paste the new code and errors so that you get a better idea
@isTest
private class testProcurementExtension{
Private static testMethod void ProcurementExtension(){
Procurement__c objProcurement = new Procurement__c();
objProcurement.Vendor__c = 'Test Vendor Account';
insert objProcurement;
Test.startTest();
ApexPages.StandardController stdCon=new ApexPages.StandardController(objProcurement);
ProcurementExtension pex=new ProcurementExtension(stdCon);
Test.stopTest();
}
}
Errors
System.StringException: Invalid id: Test Vendor Account
Stack Trace
Class.testProcurementExtension.ProcurementExtension: line 5, column 1
You are correct Account is a lookup but the name of the field is 'Vendor_c'. so I have changed 'objProcurement.Account__c' to 'objProcurement.Vendor_c'
When I used your last code I got the below errors -
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Customer_Tin_CST__c]: [Customer_Tin_CST__c]
Stack Trace:-
Class.testProcurementExtension.ProcurementExtension: line 6, column 1
Please Note: [Customer_Tin_CST__c] is a mandatory field in the account object.
You need to add mandatory field values in test record .
I tried that but still the same error. Here the code. Please help!
@isTest
private class testProcurementExtension{
Private static testMethod void ProcurementExtension(){
Account acc=new Account();
acc.Name='TestAccount';
insert acc;
acc=[SELECT id,Name FROM Account WHERE id=:acc.Id];
System.assertEquals(acc.Name,'TestAccount');
Procurement__c objProcurement = new Procurement__c();
objProcurement.Vendor__c = acc.id;
objProcurement.Vendor__r.Customer_Tin_CST__c = 'Test CST';
insert objProcurement;
objProcurement=[SELECT id,Name FROM Procurement__c WHERE id=:objProcurement.Id];
System.assertEquals(objProcurement.Name,'Test');
Test.startTest();
ApexPages.StandardController stdCon=new ApexPages.StandardController(objProcurement);
ProcurementExtension pex=new ProcurementExtension(stdCon);
Test.stopTest();
}
}
Error:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Customer_Tin_CST__c]: [Customer_Tin_CST__c]
Error says that the field(Customer_Tin_CST__c) is mandatory ,I am not sure if it is Procurement leble or Account level .You need to check and give value to this field in test record so thatit will pass .
Let me know if it helps !!
Thanks
Manoj
It is Account Level. I understand the logic but I am not getting the syntax right. Here is what I have with still the same error. Please help me fix this.
@isTest private class testProcurementExtension{
Private static testMethod void ProcurementExtension(){
Account acc=new Account();
acc.Name='TestAccount';
insert acc;
acc=[SELECT id,Name, Customer_Tin_CST__c FROM Account WHERE id=:acc.Id];
System.assertEquals(acc.Name,'TestAccount');
Procurement__c objProcurement = new Procurement__c();
objProcurement.Vendor__c = acc.id;
objProcurement.Vendor__r.Customer_Tin_CST__c = 'Test CST';
insert objProcurement;
objProcurement=[SELECT id,Name FROM Procurement__c WHERE id=:objProcurement.Id];
System.assertEquals(objProcurement.Name,'Test');
Test.startTest();
ApexPages.StandardController stdCon=new ApexPages.StandardController(objProcurement);
ProcurementExtension pex=new ProcurementExtension(stdCon);
Test.stopTest();
}
}
With some effort I was able to get through this but now there is a new error. The accont lookup have a filter that the Account_Type__c (picklist) is either Product Vendor or Service provider. I am unable to reference this in the test class. Below is the test class I am using and error I am getting. Please help me reference this.
Test Class (without the account look up filter reference)
@isTest
private class testProcurementExtension{
Private static testMethod void ProcurementExtension(){
Account acc=new Account();
acc.Name='TestAccount';
acc.Customer_Tin_CST__c='Test CST';
insert acc;
acc=[SELECT id,Name, Customer_Tin_CST__c FROM Account WHERE id=:acc.Id];
System.assertEquals(acc.Name,'TestAccount');
System.assertEquals(acc.Customer_Tin_CST__c,'Test CST');
Procurement__c objProcurement = new Procurement__c();
objProcurement.Vendor__c = acc.id; (Is this where I insert WHERE Account_Type__c = Product Vendor / Service provider)?????
insert objProcurement;
objProcurement=[SELECT id,Name FROM Procurement__c WHERE id=:objProcurement.Id];
System.assertEquals(objProcurement.Name,'Test');
Test.startTest();
ApexPages.StandardController stdCon=new ApexPages.StandardController(objProcurement);
ProcurementExtension pex=new ProcurementExtension(stdCon);
Test.stopTest();
}
}
Error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, You can only select your vendors or service providers: [Vendor__c]