You need to sign in to do that
Don't have an account?
Doug Traster 4
Test case coverage on trigger for Mapping Strings and if, else
Hi All;
I have created a trigger that pulls Product names and puts them on the Opportunity so that I can see them in a related list on Account.
I have a test case that gets me 82%, but want to push it higher in case data or something else changes.
Here is my trigger:
trigger ShowOppProd on Opportunity (before update,before insert) {
Map<Id,Set<String>> prodToOpp = new Map<Id,Set<String>> ();
for (Opportunity opp : [SELECT Id, Opportunity_Prod__c, (SELECT Id, OpportunityId, Product2.name
FROM OpportunityLineItems)FROM Opportunity
WHERE Opportunity.Id IN: Trigger.new] ) {
Set<String> productsInOpp = new Set<String>();
for (OpportunityLineItem oli : opp.opportunityLineItems)
productsInOpp.add(oli.Product2.name);
if(prodToOpp.containsKey(opp.Id)){
Set<String> uniqueProdset = prodToOpp.get(opp.Id);
uniqueProdset.addAll(productsInOpp);
prodToOpp.put(opp.Id,uniqueProdset);
}
else
prodToOpp.put(opp.Id,productsInOpp);
}
for (Opportunity o: Trigger.new){
if(prodToOpp.containsKey(o.Id)){
String concatProd = '';
for (String p : prodToOpp.get(o.Id))
concatProd = concatProd + (concatProd.length() == 0 ? '' : '; ')+ p;
o.Opportunity_Prod__c = concatProd;
}
}
}
Here is my test case:
@isTest
public class OppProdTest {
static testMethod void TestOppProd() {
Account a = new Account (
Name = 'The High School',
Organization_Type__c = 'High School'
);
Insert a;
Product2 prod = new Product2(Name = 'Bronze',
Family = 'test'
);
Insert prod;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry (
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 500,
IsActive = true
);
Insert standardPrice;
Pricebook2 customPB = new Pricebook2(
Name = 'Custom Pricebook',
isActive = true
);
Insert customPB;
PriceBookEntry customPrice = new PricebookEntry(
Pricebook2Id = customPB.Id,
Product2Id = prod.Id,
UnitPrice = 350,
IsActive = true
);
Insert customPrice;
Opportunity opp = new Opportunity(
Account = a,
Opportunity_Prod__c = prod.Name,
Name = 'Test Opp',
StageName = 'Proposal',
CloseDate = System.today()
);
Insert opp;
OpportunityLineItem OLI = new OpportunityLineItem(
PricebookEntryId = standardPrice.Id,
OpportunityId = opp.Id,
Quantity = 1,
TotalPrice = standardPrice.UnitPrice
);
Insert OLI;
System.assertEquals('Bronze', opp.Opportunity_Prod__c);
}
}
Running the issues I am getting that these lines are not under coverage. I am at a loss on how to add this to the test. I know I probably need to add another assert somewhere, but cant figure it out.
Set<String> uniqueProdset = prodToOpp.get(opp.Id);
uniqueProdset.addAll(productsInOpp);
prodToOpp.put(opp.Id,uniqueProdset);
Any help would be most appreciated!
Thanks!
I have created a trigger that pulls Product names and puts them on the Opportunity so that I can see them in a related list on Account.
I have a test case that gets me 82%, but want to push it higher in case data or something else changes.
Here is my trigger:
trigger ShowOppProd on Opportunity (before update,before insert) {
Map<Id,Set<String>> prodToOpp = new Map<Id,Set<String>> ();
for (Opportunity opp : [SELECT Id, Opportunity_Prod__c, (SELECT Id, OpportunityId, Product2.name
FROM OpportunityLineItems)FROM Opportunity
WHERE Opportunity.Id IN: Trigger.new] ) {
Set<String> productsInOpp = new Set<String>();
for (OpportunityLineItem oli : opp.opportunityLineItems)
productsInOpp.add(oli.Product2.name);
if(prodToOpp.containsKey(opp.Id)){
Set<String> uniqueProdset = prodToOpp.get(opp.Id);
uniqueProdset.addAll(productsInOpp);
prodToOpp.put(opp.Id,uniqueProdset);
}
else
prodToOpp.put(opp.Id,productsInOpp);
}
for (Opportunity o: Trigger.new){
if(prodToOpp.containsKey(o.Id)){
String concatProd = '';
for (String p : prodToOpp.get(o.Id))
concatProd = concatProd + (concatProd.length() == 0 ? '' : '; ')+ p;
o.Opportunity_Prod__c = concatProd;
}
}
}
Here is my test case:
@isTest
public class OppProdTest {
static testMethod void TestOppProd() {
Account a = new Account (
Name = 'The High School',
Organization_Type__c = 'High School'
);
Insert a;
Product2 prod = new Product2(Name = 'Bronze',
Family = 'test'
);
Insert prod;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry (
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 500,
IsActive = true
);
Insert standardPrice;
Pricebook2 customPB = new Pricebook2(
Name = 'Custom Pricebook',
isActive = true
);
Insert customPB;
PriceBookEntry customPrice = new PricebookEntry(
Pricebook2Id = customPB.Id,
Product2Id = prod.Id,
UnitPrice = 350,
IsActive = true
);
Insert customPrice;
Opportunity opp = new Opportunity(
Account = a,
Opportunity_Prod__c = prod.Name,
Name = 'Test Opp',
StageName = 'Proposal',
CloseDate = System.today()
);
Insert opp;
OpportunityLineItem OLI = new OpportunityLineItem(
PricebookEntryId = standardPrice.Id,
OpportunityId = opp.Id,
Quantity = 1,
TotalPrice = standardPrice.UnitPrice
);
Insert OLI;
System.assertEquals('Bronze', opp.Opportunity_Prod__c);
}
}
Running the issues I am getting that these lines are not under coverage. I am at a loss on how to add this to the test. I know I probably need to add another assert somewhere, but cant figure it out.
Set<String> uniqueProdset = prodToOpp.get(opp.Id);
uniqueProdset.addAll(productsInOpp);
prodToOpp.put(opp.Id,uniqueProdset);
Any help would be most appreciated!
Thanks!
Try this test class. Let me know how it goes.
Thanks, but it doesn't change my coverage. The 3 lines I mentioned above is still not covered. I think I need to get another assert into the test in some way to get this to work.
Hi,
Could you please explain use case of below lines?
Thanks in advance