You need to sign in to do that
Don't have an account?
Silpi roy 16
Test class for trigger with helper class
I have written the below code trigger and test class but getting error in deployment as 0%test coverage.
rigger Product_StatusChangeAlert on Product2 (after update) {
if(Process_Exception__c.getInstance(Userinfo.getProfileId()).Run_Trigger__c){
List<Product2> listStatusChanged = new List<Product2>();
for (Product2 o : trigger.new) {
if (o.Product_Status2__c != trigger.oldMap.get(o.ID).Product_Status2__c && ProductStatusChangeAlert__c.getInstance(o.Product_Status2__c) != null){
listStatusChanged.add(o);
}
}
if (!listStatusChanged.isEmpty()) {
ProductStatusChangeAlertController helper = new ProductStatusChangeAlertController(listStatusChanged);
helper.sendEmailsToOpportunityOwners();
}
}
Test Class:
@isTest
public class ProductStatusChangeAlertControllerTest {
@isTest static void Product2StatusChangedTest() {
// Insert "Process_Exception__c" related record with "System Administrator" profile
Process_Exception__c procExcpRec = new Process_Exception__c(Name = 'Process Exception (Profile)',Run_Trigger__c = true, SetupOwnerId = '00e0Y0000015jIAQAY');
insert procExcpRec;
ProductStatusChangeAlert__c prodStaChaAlRec = new ProductStatusChangeAlert__c(Name = 'End of Life');
insert prodStaChaAlRec;
Product2 prd = new Product2(Name='Test Product', IsActive=true, MSTAV__c = '80');
insert prd;
Id stdPbId = Test.getStandardPricebookId();
PricebookEntry objPBE = new PricebookEntry(Product2Id=prd.Id, Pricebook2Id=stdPbId, UnitPrice=100, IsActive=true);
insert objPBE;
List<Opportunity> tobeInsertedOpptyList = new List<Opportunity>();
Opportunity objOpp = new Opportunity(Name = 'Testopp', CloseDate = date.today().addDays(100), StageName = 'Prospecting');
tobeInsertedOpptyList.add(objOpp);
Opportunity objOpp2 = new Opportunity(Name = 'Testopp2', CloseDate = date.today().addDays(100), StageName = 'Prospecting');
tobeInsertedOpptyList.add(objOpp2);
insert tobeInsertedOpptyList;
OpportunityLineItem objOLI = new OpportunityLineItem(PricebookEntryID = objPBE.Id, Quantity = 1, UnitPrice = 100, OpportunityId = objOpp.Id);
insert objOLI;
OpportunityLineItem objOLI2 = new OpportunityLineItem(PricebookEntryID = objPBE.Id, Quantity = 1, UnitPrice = 100, OpportunityId = objOpp2.Id);
insert objOLI2;
Product2 objProduct = [select Id, Product_Status2__c,MSTAV__c from Product2 where Id = :objPBE.Product2Id];
objProduct.MSTAV__c = '80';
update objProduct;
}
Please advise.
Thanks,
Silpi
rigger Product_StatusChangeAlert on Product2 (after update) {
if(Process_Exception__c.getInstance(Userinfo.getProfileId()).Run_Trigger__c){
List<Product2> listStatusChanged = new List<Product2>();
for (Product2 o : trigger.new) {
if (o.Product_Status2__c != trigger.oldMap.get(o.ID).Product_Status2__c && ProductStatusChangeAlert__c.getInstance(o.Product_Status2__c) != null){
listStatusChanged.add(o);
}
}
if (!listStatusChanged.isEmpty()) {
ProductStatusChangeAlertController helper = new ProductStatusChangeAlertController(listStatusChanged);
helper.sendEmailsToOpportunityOwners();
}
}
Test Class:
@isTest
public class ProductStatusChangeAlertControllerTest {
@isTest static void Product2StatusChangedTest() {
// Insert "Process_Exception__c" related record with "System Administrator" profile
Process_Exception__c procExcpRec = new Process_Exception__c(Name = 'Process Exception (Profile)',Run_Trigger__c = true, SetupOwnerId = '00e0Y0000015jIAQAY');
insert procExcpRec;
ProductStatusChangeAlert__c prodStaChaAlRec = new ProductStatusChangeAlert__c(Name = 'End of Life');
insert prodStaChaAlRec;
Product2 prd = new Product2(Name='Test Product', IsActive=true, MSTAV__c = '80');
insert prd;
Id stdPbId = Test.getStandardPricebookId();
PricebookEntry objPBE = new PricebookEntry(Product2Id=prd.Id, Pricebook2Id=stdPbId, UnitPrice=100, IsActive=true);
insert objPBE;
List<Opportunity> tobeInsertedOpptyList = new List<Opportunity>();
Opportunity objOpp = new Opportunity(Name = 'Testopp', CloseDate = date.today().addDays(100), StageName = 'Prospecting');
tobeInsertedOpptyList.add(objOpp);
Opportunity objOpp2 = new Opportunity(Name = 'Testopp2', CloseDate = date.today().addDays(100), StageName = 'Prospecting');
tobeInsertedOpptyList.add(objOpp2);
insert tobeInsertedOpptyList;
OpportunityLineItem objOLI = new OpportunityLineItem(PricebookEntryID = objPBE.Id, Quantity = 1, UnitPrice = 100, OpportunityId = objOpp.Id);
insert objOLI;
OpportunityLineItem objOLI2 = new OpportunityLineItem(PricebookEntryID = objPBE.Id, Quantity = 1, UnitPrice = 100, OpportunityId = objOpp2.Id);
insert objOLI2;
Product2 objProduct = [select Id, Product_Status2__c,MSTAV__c from Product2 where Id = :objPBE.Product2Id];
objProduct.MSTAV__c = '80';
update objProduct;
}
Please advise.
Thanks,
Silpi