function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SF7SF7 

Test Class Coverage issue

trigger ConversionRate on OpportunityLineItem (before insert, before update) {
List<String> LineofBusiness = new List<String>(); 
for (OpportunityLineItem a:Trigger.new)
{
LineofBusiness.add(a.Hidden_Name__c);

}
List <Win_Rate__c> WinRateList = [Select ID,Name, Line_of_Business__c,Country__C from Win_Rate__c where Name in :LineofBusiness];
for (Integer i = 0; i <Trigger.new.size(); i++){

    if (WinRateList.size() > 0 && Trigger.new[i].Hidden_Name__c !=null) {
                for (Win_Rate__c W:WinRateList){
    if (Trigger.new[i].Hidden_Name__c == W.name){

           Trigger.new[i].WinRate__c= W.ID;
     }
     }
     }
else{
Trigger.new[i].WinRate__c = null;
}


}
}
@isTest(seeAllData=true)
private class TestConversionRate {

 @isTest static void MyUnitTest() {
 // Set up some local variables
String opportunityName = 'My Opportunity';
String standardPriceBookId = '';

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
standardPriceBookId = pb2Standard.Id;

Account a = new Account(Name = 'Berlington');
  Insert a;
  
// set up opp and Verify that the results are as expected.
Opportunity o = new Opportunity(AccountId = a.id, Name=opportunityName, 
StageName='Discovery', CloseDate=Date.today());
insert o;
Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id];
//System.assertEquals(opportunityName, 'Berlington - 0 -');

// set up product2 and Verify that the results are as expected.
Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
//System.assertEquals('Test Product', p2ex.Name);

// set up PricebookEntry and Verify that the results are as expected.
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;
PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
//System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);

Win_Rate__c w = new Win_Rate__c(Name = 'Manpower - Brazil - 121-180',Win_rate__c = 90 );
  Insert W;

// set up OpportunityLineItem and Verify that the results are as expected.
OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id,WinRate__c = w.id, Quantity=1, TotalPrice=99);
insert oli;

OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId); 
 
 
 opportunityLineItem Oppl = [Select Id,Name ,WinRate__c from OpportunityLineItem where id =:oli.id];
 
 system.assertnotEquals(oppl.WinRate__c ,'');      
 }

Only Getting 70% and any insight what i am missing?
Michael VerhovskiMichael Verhovski
What lines are not covered by your tests?
SF7SF7
Hey Micheal,

Thanks for the reply , i was able to get  100% Coverage.


SF7SF7
@isTest(seeAllData=true)
private class TestConversionRate {

 @isTest static void MyUnitTest() {
 // Set up some local variables
String opportunityName = 'My Opportunity';
String standardPriceBookId = '';

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
standardPriceBookId = pb2Standard.Id;

Account a = new Account(Name = 'Berlington');
  Insert a;
  
// set up opp and Verify that the results are as expected.
Opportunity o = new Opportunity(AccountId = a.id, Name=opportunityName, 
StageName='Discovery', CloseDate=Date.today());
insert o;
Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id];
//System.assertEquals(opportunityName, 'Berlington - 0 -');

// set up product2 and Verify that the results are as expected.
Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
//System.assertEquals('Test Product', p2ex.Name);

// set up PricebookEntry and Verify that the results are as expected.
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;
PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
//System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);

Win_Rate__c w = new Win_Rate__c(Name = 'Manpower - Brazil - 121-180',Win_rate__c = 90 );
  Insert W;

// set up OpportunityLineItem and Verify that the results are as expected.
OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id,WinRate__c = w.id,Hidden_Name__c = w.name, Quantity=1, TotalPrice=99);
insert oli;

OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId); 
 
 
 opportunityLineItem Oppl = [Select Id,Name ,WinRate__c ,Hidden_Name__c from OpportunityLineItem where id =:oli.id];
 
 system.assertnotEquals(oppl.WinRate__c ,'');      
 }

}
But only Pb is , i did not understand why my assert is failing in the last statement.
Michael VerhovskiMichael Verhovski
I would check the value of oppl.WinRate__c  to make sure that the code is executing the way you think and check it in Debug log

System.Debug('>>>> the value of oppl.WinRate__c is ' + oppl.WinRate__c);