You need to sign in to do that
Don't have an account?
praveen avvari
Question Regarding Apex Test Class coverage
Hi , My test class is not coverging all of the code.
can you please help me what is missing here, so that i can get 100% coverage
Apex Test Class:
--------------------------------------------------------------------------------------
@isTest
public class QuoteTriggerTest {
private static Map<String, Id> getRecordTypes() {
Map<String, Id> recordTypeByName = new Map<String, Id>();
List<RecordType> rts = [select id,developerName from RecordType];
system.debug(rts);
for(RecordType r: rts) {
recordTypeByName.put(r.developerName, r.Id);
}
return recordTypeByName;
}
@isTest static void TestQuote() {
Map<String, Id> rts = getRecordTypes();
Product2 p = TestDataFactory.createProduct('Test1', 'Goods', 'Food goods', 'Fixed Amount', true);
p.IsActive = true;
insert p;
Id pbId = Test.getStandardPricebookId();
PriceBookEntry pb = TestDataFactory.creatPriceBook(p.Id, 10, true, pbId);
insert pb;
Account company = TestDataFactory.createAccount('Au_feminin1','test1@gmail.com', 'France', 'Paris', 'Street', 'France', 'Paris', 'Paris street', 'LUX-TVA', '2211', rts.get('Company'));
insert company;
Account acc = TestDataFactory.createAccount('Apple1','test2@gmail.com', 'Belgium', 'Liege', 'Street', 'Belgium', 'Liege', 'street', 'BHS-TIN', '223', rts.get('Annonceur'));
insert acc;
Brand__c br = new Brand__c(
name = 'brand 2', Account__c = acc.id
);
insert br;
Contact con = new Contact(LastName = 'contact 2');
insert con;
Opportunity opp = new Opportunity(
Name = 'Testing',
StageName = 'Qualification',
closeDate = System.today() + 50,
Company__c = company.id,
accountId = acc.id,
recordtypeId = rts.get('Programmatic'),
Sector__c = 'CW1_SECTOR-81',
Contract_Type__c = 'CUSTOMER_CONTRACT_TYPE-8',
SyncedQuoteId = null
//SyncedQuoteId = q.id
);
insert opp;
OpportunityLineItem OppLineItem = new OpportunityLineItem(
OpportunityID=opp.id,
PriceBookEntryID=pb.Id,
Product2Id = p.id,
ServiceDate = System.today() + 500,
Realization_to_date__c = System.today() + 510,
UnitPrice = 50,
Quantity=4,
Cost_Center_Company__c = 'Alfemminile',
Cost_Center__c = 'CC-ALF51'
);
insert OppLineItem;
SetStaticVariable.AutomaticCreation = true;
OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule(ActualCosts__c = 100, EstimatedCost__c = 200, OpportunityLineItemId = OppLineItem.Id,
Type='Revenue', Revenue = 200,ScheduleDate = System.today()+20,
FromDate__c = system.today()+20, EndDate__c = system.today() + 80,
Revenue_Recognition_Product__c = 'Custom');
insert olis;
Quote q = new Quote( //AccountId = acc.Id,
Advertiser_Contact__c = con.id,
Agency_Contact__c = con.id,
Brand__c = br.id,
status = 'Review Pending',
shippingCountry = 'Belgium',
name = 'Test Quote 1',
OpportunityId = opp.id,
Pricebook2Id = pbId
// IsSyncing = true
);
insert q;
QuoteLineItem qli = TestDataFactory.createQuoteLineItem(q.id, 1, 1, pb.Id, opp.Id, OppLineItem.Id);
insert qli;
Billing_Schedule__c bil = new Billing_Schedule__c(
Opportunity__c = opp.id,
Amount__c = 20.5,
Synched_Quote__c = q.id,
Opportunity_Product_Id__c = OppLineItem.Id,
Start_Date__c = System.today() + 500,
End_Date__c = System.today() + 510
);
insert bil;
//opp.SyncedQuoteId = q.Id;
//update opp;
/*
q.status = 'SentForApproval';
update q;
*/
/* q.IsSyncing = true;
update q;
*/
}
}
-----------------------------------------------------------------------------------
Apex Trigger
------------------------------------------------------------------------------------
trigger QuoteTrigger on Quote (before Update)
{
for (Quote Quote : Trigger.new)
{
//Bloquer l'avancement de la Quote suivant le statut des Schedule Item et Billing Schedule
if (Trigger.oldMap.get(Quote.Id).Status != Quote.Status && Quote.Status == 'Sent For Approval')
{
//Selectionner toutes les lignes d'opp (hors produits offerts) avec leurs Revenues Schedule
List<OpportunityLineItem> OppLineItem_1 = [ SELECT id, (SELECT Id from OpportunityLineItemSchedules)
FROM OpportunityLineItem
WHERE OpportunityId =: Quote.OpportunityId AND
Discount != 100];
//Selectionner toutes les lignes d'opp (hors produits offerts) avec leurs Revenues Schedule dont le Estimated Cost = Null
List<OpportunityLineItem> OppLineItem_2 = [ SELECT id, (SELECT Id, EstimatedCost__c from OpportunityLineItemSchedules where EstimatedCost__c = null)
FROM OpportunityLineItem
WHERE OpportunityId =: Quote.OpportunityId AND
Discount != 100];
//Start Ticket 603 ,Praveen, October 6th,2020
//Salesforce cosiders Organisation curency when we use Aggregate functions,in order to over come we are using this fix
//Assumtions:since we ar using Dated Exchange rates, all Values in manage Currencies to be set to 1
Opportunity opp = [SELECT CurrencyIsoCode,CloseDate FROM Opportunity WHERE Id =: Quote.OpportunityId];
Decimal cts = [SELECT Id, ConversionRate FROM DatedConversionRate WHERE IsoCode = :opp.CurrencyIsoCode AND StartDate <= :opp.CloseDate AND NextStartDate > :opp.CloseDate].ConversionRate;
//End Ticket 603
//Si ligne d'opp existe
if (OppLineItem_1 != null && OppLineItem_1.size() > 0)
{
for (OpportunityLineItem OppLine_1 : OppLineItem_1)
{
List<OpportunityLineItemSchedule> LineItemSchedule_1 = OppLine_1.OpportunityLineItemSchedules;
//Vérifier s'il y aucun Revenue Schedule crée
if (LineItemSchedule_1 == null || LineItemSchedule_1.size() == 0)
{
quote.adderror(System.Label.ScheduleErrorMsg3);
return;
}
}
for (OpportunityLineItem OppLine_2 : OppLineItem_2)
{
List<OpportunityLineItemSchedule> LineItemSchedule_2 = OppLine_2.OpportunityLineItemSchedules;
//Vérifier s'il y au moins un Revenue Schedule avec un Estimated Cost = null
if (LineItemSchedule_2.size() > 0)
{
quote.adderror(System.Label.ScheduleErrorMsg4);
return;
}
}
}
//Vérifier si sur chaque ligne d'opportunité a au moins une ligne de Billing Schedule
//Récuperer toutes lignes de billing schedule par rapport à l'opportunité de la quote
Map<Id,AggregateResult> BillingSchedule = new Map<Id, AggregateResult>([ SELECT SUM(Amount__c) Amounts, Opportunity_Product_Id__c Id
FROM Billing_Schedule__c
WHERE Opportunity__c =: Quote.OpportunityId
GROUP BY Opportunity_Product_Id__c
]);
//Récuperer toutes les lignes d'opportunité qui n'ont pas de billing schedule
List <OpportunityLineItem> OppLineIds = [SELECT id, Discount FROM OpportunityLineItem WHERE OpportunityId =: Quote.OpportunityId and id NOT IN : BillingSchedule.KeySet() and Discount != 100];
if (!OppLineIds.IsEmpty())
{
quote.adderror(System.Label.ScheduleErrorMsg5);
return;
}
else
{
//Récupérer la somme de TotalPrice des lignes d'opp rattachées à l'opportunité
Map<Id,AggregateResult> OppLineItem_3 = new Map<Id, AggregateResult>([ SELECT OpportunityId Id, SUM(TotalPrice) Totals
FROM OpportunityLineItem
WHERE OpportunityId =: Quote.OpportunityId AND
Discount != 100
GROUP BY OpportunityId
]);
decimal TotalBillingAmount = 0.00;
//Additionner les montants des Billing Schedule
for (Id key : BillingSchedule.keySet())
{
TotalBillingAmount = TotalBillingAmount + (Decimal)BillingSchedule.get(key).get('Amounts');
TotalBillingAmount = (TotalBillingAmount * 1/cts).setScale(2, RoundingMode.HALF_UP); //Ticket 603
}
//Vérifier si le montant des TotalPrice est égal ou non à la somme des montants des Billings Schedule
for (Id key : OppLineItem_3.keySet())
{
if ( (Decimal)OppLineItem_3.get(key).get('Totals') != TotalBillingAmount)
{
quote.adderror(System.Label.ScheduleErrorMsg12);
}
}
}
//Fin de vérification
}
}
}
------------------------------------------------------------------------------------
can you please help me what is missing here, so that i can get 100% coverage
Apex Test Class:
--------------------------------------------------------------------------------------
@isTest
public class QuoteTriggerTest {
private static Map<String, Id> getRecordTypes() {
Map<String, Id> recordTypeByName = new Map<String, Id>();
List<RecordType> rts = [select id,developerName from RecordType];
system.debug(rts);
for(RecordType r: rts) {
recordTypeByName.put(r.developerName, r.Id);
}
return recordTypeByName;
}
@isTest static void TestQuote() {
Map<String, Id> rts = getRecordTypes();
Product2 p = TestDataFactory.createProduct('Test1', 'Goods', 'Food goods', 'Fixed Amount', true);
p.IsActive = true;
insert p;
Id pbId = Test.getStandardPricebookId();
PriceBookEntry pb = TestDataFactory.creatPriceBook(p.Id, 10, true, pbId);
insert pb;
Account company = TestDataFactory.createAccount('Au_feminin1','test1@gmail.com', 'France', 'Paris', 'Street', 'France', 'Paris', 'Paris street', 'LUX-TVA', '2211', rts.get('Company'));
insert company;
Account acc = TestDataFactory.createAccount('Apple1','test2@gmail.com', 'Belgium', 'Liege', 'Street', 'Belgium', 'Liege', 'street', 'BHS-TIN', '223', rts.get('Annonceur'));
insert acc;
Brand__c br = new Brand__c(
name = 'brand 2', Account__c = acc.id
);
insert br;
Contact con = new Contact(LastName = 'contact 2');
insert con;
Opportunity opp = new Opportunity(
Name = 'Testing',
StageName = 'Qualification',
closeDate = System.today() + 50,
Company__c = company.id,
accountId = acc.id,
recordtypeId = rts.get('Programmatic'),
Sector__c = 'CW1_SECTOR-81',
Contract_Type__c = 'CUSTOMER_CONTRACT_TYPE-8',
SyncedQuoteId = null
//SyncedQuoteId = q.id
);
insert opp;
OpportunityLineItem OppLineItem = new OpportunityLineItem(
OpportunityID=opp.id,
PriceBookEntryID=pb.Id,
Product2Id = p.id,
ServiceDate = System.today() + 500,
Realization_to_date__c = System.today() + 510,
UnitPrice = 50,
Quantity=4,
Cost_Center_Company__c = 'Alfemminile',
Cost_Center__c = 'CC-ALF51'
);
insert OppLineItem;
SetStaticVariable.AutomaticCreation = true;
OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule(ActualCosts__c = 100, EstimatedCost__c = 200, OpportunityLineItemId = OppLineItem.Id,
Type='Revenue', Revenue = 200,ScheduleDate = System.today()+20,
FromDate__c = system.today()+20, EndDate__c = system.today() + 80,
Revenue_Recognition_Product__c = 'Custom');
insert olis;
Quote q = new Quote( //AccountId = acc.Id,
Advertiser_Contact__c = con.id,
Agency_Contact__c = con.id,
Brand__c = br.id,
status = 'Review Pending',
shippingCountry = 'Belgium',
name = 'Test Quote 1',
OpportunityId = opp.id,
Pricebook2Id = pbId
// IsSyncing = true
);
insert q;
QuoteLineItem qli = TestDataFactory.createQuoteLineItem(q.id, 1, 1, pb.Id, opp.Id, OppLineItem.Id);
insert qli;
Billing_Schedule__c bil = new Billing_Schedule__c(
Opportunity__c = opp.id,
Amount__c = 20.5,
Synched_Quote__c = q.id,
Opportunity_Product_Id__c = OppLineItem.Id,
Start_Date__c = System.today() + 500,
End_Date__c = System.today() + 510
);
insert bil;
//opp.SyncedQuoteId = q.Id;
//update opp;
/*
q.status = 'SentForApproval';
update q;
*/
/* q.IsSyncing = true;
update q;
*/
}
}
-----------------------------------------------------------------------------------
Apex Trigger
------------------------------------------------------------------------------------
trigger QuoteTrigger on Quote (before Update)
{
for (Quote Quote : Trigger.new)
{
//Bloquer l'avancement de la Quote suivant le statut des Schedule Item et Billing Schedule
if (Trigger.oldMap.get(Quote.Id).Status != Quote.Status && Quote.Status == 'Sent For Approval')
{
//Selectionner toutes les lignes d'opp (hors produits offerts) avec leurs Revenues Schedule
List<OpportunityLineItem> OppLineItem_1 = [ SELECT id, (SELECT Id from OpportunityLineItemSchedules)
FROM OpportunityLineItem
WHERE OpportunityId =: Quote.OpportunityId AND
Discount != 100];
//Selectionner toutes les lignes d'opp (hors produits offerts) avec leurs Revenues Schedule dont le Estimated Cost = Null
List<OpportunityLineItem> OppLineItem_2 = [ SELECT id, (SELECT Id, EstimatedCost__c from OpportunityLineItemSchedules where EstimatedCost__c = null)
FROM OpportunityLineItem
WHERE OpportunityId =: Quote.OpportunityId AND
Discount != 100];
//Start Ticket 603 ,Praveen, October 6th,2020
//Salesforce cosiders Organisation curency when we use Aggregate functions,in order to over come we are using this fix
//Assumtions:since we ar using Dated Exchange rates, all Values in manage Currencies to be set to 1
Opportunity opp = [SELECT CurrencyIsoCode,CloseDate FROM Opportunity WHERE Id =: Quote.OpportunityId];
Decimal cts = [SELECT Id, ConversionRate FROM DatedConversionRate WHERE IsoCode = :opp.CurrencyIsoCode AND StartDate <= :opp.CloseDate AND NextStartDate > :opp.CloseDate].ConversionRate;
//End Ticket 603
//Si ligne d'opp existe
if (OppLineItem_1 != null && OppLineItem_1.size() > 0)
{
for (OpportunityLineItem OppLine_1 : OppLineItem_1)
{
List<OpportunityLineItemSchedule> LineItemSchedule_1 = OppLine_1.OpportunityLineItemSchedules;
//Vérifier s'il y aucun Revenue Schedule crée
if (LineItemSchedule_1 == null || LineItemSchedule_1.size() == 0)
{
quote.adderror(System.Label.ScheduleErrorMsg3);
return;
}
}
for (OpportunityLineItem OppLine_2 : OppLineItem_2)
{
List<OpportunityLineItemSchedule> LineItemSchedule_2 = OppLine_2.OpportunityLineItemSchedules;
//Vérifier s'il y au moins un Revenue Schedule avec un Estimated Cost = null
if (LineItemSchedule_2.size() > 0)
{
quote.adderror(System.Label.ScheduleErrorMsg4);
return;
}
}
}
//Vérifier si sur chaque ligne d'opportunité a au moins une ligne de Billing Schedule
//Récuperer toutes lignes de billing schedule par rapport à l'opportunité de la quote
Map<Id,AggregateResult> BillingSchedule = new Map<Id, AggregateResult>([ SELECT SUM(Amount__c) Amounts, Opportunity_Product_Id__c Id
FROM Billing_Schedule__c
WHERE Opportunity__c =: Quote.OpportunityId
GROUP BY Opportunity_Product_Id__c
]);
//Récuperer toutes les lignes d'opportunité qui n'ont pas de billing schedule
List <OpportunityLineItem> OppLineIds = [SELECT id, Discount FROM OpportunityLineItem WHERE OpportunityId =: Quote.OpportunityId and id NOT IN : BillingSchedule.KeySet() and Discount != 100];
if (!OppLineIds.IsEmpty())
{
quote.adderror(System.Label.ScheduleErrorMsg5);
return;
}
else
{
//Récupérer la somme de TotalPrice des lignes d'opp rattachées à l'opportunité
Map<Id,AggregateResult> OppLineItem_3 = new Map<Id, AggregateResult>([ SELECT OpportunityId Id, SUM(TotalPrice) Totals
FROM OpportunityLineItem
WHERE OpportunityId =: Quote.OpportunityId AND
Discount != 100
GROUP BY OpportunityId
]);
decimal TotalBillingAmount = 0.00;
//Additionner les montants des Billing Schedule
for (Id key : BillingSchedule.keySet())
{
TotalBillingAmount = TotalBillingAmount + (Decimal)BillingSchedule.get(key).get('Amounts');
TotalBillingAmount = (TotalBillingAmount * 1/cts).setScale(2, RoundingMode.HALF_UP); //Ticket 603
}
//Vérifier si le montant des TotalPrice est égal ou non à la somme des montants des Billings Schedule
for (Id key : OppLineItem_3.keySet())
{
if ( (Decimal)OppLineItem_3.get(key).get('Totals') != TotalBillingAmount)
{
quote.adderror(System.Label.ScheduleErrorMsg12);
}
}
}
//Fin de vérification
}
}
}
------------------------------------------------------------------------------------
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
It has a very clear explanation.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.