You need to sign in to do that
Don't have an account?
Kiran G
Help Test Class for an extension
Hi Everyone,
I need help with a test class for the apex class below. I have got to somewhere with some help but I am still getiing errors with the code.
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:
@isTest
private class testProcurementExtension{
Private static testMethod void ProcurementExtension(){
Account acc=new Account(Account_Type__c='Product Vendor'); {need to also add Account_Type__c='Service Provider' which is also a filter criteria}
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;
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.AssertException: Assertion Failed: Expected: JLS P - 121215 - 100001, Actual: Test
Please note:
Vendor_c is a lookup to accounts with crieteria that the Account_Type__c='Product Vendor' or 'Service Provider''
Procurement.Name is a auto number.
Please help!
I need help with a test class for the apex class below. I have got to somewhere with some help but I am still getiing errors with the code.
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:
@isTest
private class testProcurementExtension{
Private static testMethod void ProcurementExtension(){
Account acc=new Account(Account_Type__c='Product Vendor'); {need to also add Account_Type__c='Service Provider' which is also a filter criteria}
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;
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.AssertException: Assertion Failed: Expected: JLS P - 121215 - 100001, Actual: Test
Please note:
Vendor_c is a lookup to accounts with crieteria that the Account_Type__c='Product Vendor' or 'Service Provider''
Procurement.Name is a auto number.
Please help!
Instead, I would add one (or more) System.assert calls after Test.stopTest. In place of your System.assert, try adding the following after Test.stopTest:
Thanks I have removed the below code from my test class
" insert objProcurement;
objProcurement=[SELECT id,Name FROM Procurement__c WHERE id=:objProcurement.Id];
System.assertEquals(objProcurement.Name,'Test'); "
And inserted " System.assert(!pex.availableFields.isEmpty());" after Test.stop Test;
But Now I am getting a new error
"System.SObjectException: You cannot call reset when the data is being passed into the controller by the caller."
I am not much of a coder please help!