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
Chidanand MChidanand M 

Test coverage issue (Covers only 73%)

Hi friends,

I could cover only 73% of code.
Code insde the for loop of pricebook entry is not covered.
Select query of opportunity is returning me 0 rows inside the test class.
Apex

global class RenewexistingOpp implements Schedulable 
{
   global void execute(SchedulableContext SC) 
   {
       NewOppFromexistingOpp Opp= new NewOppFromexistingOpp();
   }
   public class NewOppFromexistingOpp 
   {
       NewOppFromexistingOpp()
       {      
           Date d = Date.Today();
           Date ssdt= Date.newInstance(2014,1,1);
           User newUser=[select Id from user where Name='Jarin Stevens'];

            for(Opportunity oldOpp:[select Name,Amount,AccountId,Service_End_Date__c,Service_Start_Date__c,
            CloseDate,StageName,AutoProcessedFlag__c,Why_OP_Win_Details__c,Billing_Contact__c,Primary_Contact__c,
            Payment_Terms__c,Billing_Cycle__c from opportunity where StageName='Closed Won' And AutoProcessedFlag__c=false])
    {
   
    
                System.debug(oldOpp);
                if(oldOpp.Service_Start_Date__c!=NULL)
                {
        
                        if(oldOpp.Service_Start_Date__c >=ssdt && oldOpp.Service_End_Date__c>=d )  
                        {  
        
                            Opportunity newOpp= new Opportunity();
                            newOpp.Name=oldOpp.Name; 
                            newOpp.OwnerId=newUser.Id;
                            newOpp.CloseDate=oldOpp.Service_End_Date__c + 1;  
                            newOpp.StageName='Renewal - Decision';  

                            insert newOpp;
                             for(PriceBookEntry priceBookList:[select Id,UnitPrice,Pricebook2Id,Product2Id FROM PriceBookEntry where Product2.Name IN('Enterprise License','ObservePoint Platform - Contracted') And PriceBook2.isStandard=true])
                            {
                                OpportunityLineItem oli = new OpportunityLineItem(); // Create new Opportunity Product
                                oli.OpportunityId = newOpp.Id;                           
                                oli.PricebookEntryId=priceBookList.Id;       
                                oli.UnitPrice =priceBookList.UnitPrice;                       
                                oli.Quantity=1;
                                insert oli;
                                
                            }
                            oldOpp.AutoProcessedFlag__c = true;
                           
                            upsert oldOpp;

                      }
                       
                 }
             
           }  
       }
    }
}
Test Class

@isTest (SeeAllData=false)

public class RenewexistingOppTest{

public static String CRON_EXP = '0 0 0 15 3 ? 2022';

    
    static testmethod void RenewexistingOppTest() {
    Test.startTest();
    Date sed = Date.newInstance(2015, 9, 15);
    Date ssd= Date.newInstance(2014,1,1);
    Date cd= Date.newInstance(2017, 9, 15);
    Date d = Date.Today();
    List<PricebookEntry > pntr= new List<PricebookEntry >();
    
    Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='wwy', LanguageLocaleKey='en_US',FirstName='wwx',
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='abvc@gmail.com');
    
     insert u;
     
     User uid=[select Id from User where Name='wwx wwy' LIMIT 1];
    
    
      
    Account createAcc= new Account(Name='testAccount'); 
    insert createAcc;
    
    Campaign cmp = new Campaign(Name='Chidanand');
    insert cmp;
    
    Contact createCont=new Contact(FirstName='testContact1',LastName='testContact1',AccountId=createAcc.Id);
    insert createCont;
    
    
    Opportunity createOpp= new Opportunity(Name='TestNew2',Amount=500.00,AccountId=createAcc.Id,Service_End_Date__c=sed,
                                           Service_Start_Date__c=ssd,CampaignId=cmp.Id,NextStep='hi',CloseDate=cd,StageName='Closed Won',AutoProcessedFlag__c=false,
                                           Why_OP_Win_Details__c='Hi',Billing_Contact__c=createCont.Id,Primary_Contact__c=createCont.Id,
                                           Payment_Terms__c='Net 15',Billing_Cycle__c='Annual In Advance');  
    
                                  
    insert createOpp;
    
      
    Product2 createProd=new Product2(Name='ObservePoint Platform - Contracted');
    insert createProd; 
    
   // Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true]; 
        
        Id pricebookId = Test.getStandardPricebookId();                               
    
  //  PricebookEntry createPE=new PricebookEntry(Pricebook2Id=stdpb.Id,Product2Id=createProd.Id,UnitPrice=500,IsActive = true);
     
     PricebookEntry createPE=new PricebookEntry(Pricebook2Id=pricebookId,Product2Id=createProd.Id,UnitPrice=500,IsActive = true);
     insert createPE;
    
    String jobId = System.schedule('Schedule Opp From Opp',CRON_EXP,new RenewexistingOpp());
                        
    CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
         
         System.assertEquals(CRON_EXP, ct.CronExpression);
         
         System.assertEquals(0, ct.TimesTriggered);
         System.assertEquals('2022-03-15 00:00:00', String.valueOf(ct.NextFireTime));
         Test.stopTest();  
             
         for(Opportunity oldOpp:[select Id,Name,Amount,AccountId,Service_End_Date__c,Service_Start_Date__c,
           CloseDate,StageName,AutoProcessedFlag__c,NextStep,CampaignId,Why_OP_Win_Details__c,Billing_Contact__c,Primary_Contact__c,
           Payment_Terms__c,Billing_Cycle__c from opportunity where StageName=:'Closed Won' And AutoProcessedFlag__c=:false]){
           
           System.debug('hi1'); 
           if(oldOpp.Service_Start_Date__c!=NULL){
        
         System.debug('hi2'); 
                             if(oldOpp.Service_start_Date__c>=ssd && oldOpp.Service_End_Date__c>= d)  
                        {  
                        
                         System.debug('hi3'); 
                            Opportunity newOpp= new Opportunity();
                            newOpp.Name=oldOpp.Name; 
                            newOpp.StageName='Renewal - Decision'; 
                            
                            newOpp.OwnerId=uid.Id;
                           // newOpp.AccountId=oldOpp.AccountId; 
                            newOpp.CloseDate=oldOpp.Service_End_Date__c+1;    
                             insert newOpp;
                           
                           System.debug('hello');  
                           system.debug([select Id,UnitPrice,Pricebook2Id,Product2Id FROM PriceBookEntry where Product2.Name IN ('Enterprise License','ObservePoint Platform - Contracted')]);
                             
                             for(PriceBookEntry priceBookList:[select Id,UnitPrice,Pricebook2Id,Product2Id FROM PriceBookEntry])
                            {
                            
                                //system.debug([select Id,UnitPrice,Pricebook2Id,Product2Id FROM PriceBookEntry where Product2.Name IN ('Enterprise License','ObservePoint Platform - Contracted')]);
                            
                                OpportunityLineItem oli = new OpportunityLineItem(); // Create new Opportunity Product
                                oli.OpportunityId = newOpp.Id;                           
                                oli.PricebookEntryId=priceBookList.Id; 
                               // oli.PricebookEntryId='00kJ0000008VXct';
                                oli.UnitPrice =priceBookList.UnitPrice;                       
                                oli.Quantity=1;
                                insert oli;
                                
                            }
                             
                            oldOpp.AutoProcessedFlag__c = true;
                              
                            upsert oldOpp;
                        
                        }
           }
           
           }
         
                 
    
    
    }
     
    }



 
Sunil PalSunil Pal
HI Chidu Magu,

Can you please try this :
for(Opportunity oldOpp:[select Id,Name,Amount,AccountId,Service_End_Date__c,Service_Start_Date__c,

           CloseDate,StageName,AutoProcessedFlag__c,NextStep,CampaignId,Why_OP_Win_Details__c,Billing_Contact__c,Primary_Contact__c,
      Payment_Terms__c,Billing_Cycle__c from opportunity where Id =: createOpp.Id AND StageName=:'Closed Won' And AutoProcessedFlag__c=:false])

Thanks,
Sunil
TopSSDs TopSSDsTopSSDs TopSSDs
I think my search for the best SSDs ended in your article.

I was really worried because my PC was draining out my stress by working like a snail. I was even not able to complete my work on time. Having a good speed system has become compulsory in this Work from Home situation.

I was thinking about changing my PC, but instead got an idea of trying replacing Hard Drive with an SSD. Thanks to you dear blogger, I got the Top 7 SSDs list in your article. Now it will be easy for me to select amongst those!
 
vishal kalevishal kale
I was finding a solution for my gaming laptop (https://bestbuyingdeal.in/best-gaming-laptop-under-60000-with-gtx-1650/) and I found this while surfing on the web for info thanks for giving such valuable info that worked for my Laptop (https://bestbuyingdeal.in/best-laptop-for-engineering-students-in-india/)