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
Cherry Shen 1Cherry Shen 1 

Can't pass Apex test

We have an active "Invoice Manager" in production, now we need to make some changes in it, so we're ready to modify the relevant Apex class in Sandbox first, but before that, we execute the apex test without any modification, and get an error with the origianl apex files. How does it happen as they are active in production in the meantime? Can anybody tell me if something important we've missed?
User-added image

Here is the test file, the log message above shows the error occurs in the line "insert new List<OpportunityLineItem>{testProduct1,testProduct2};", is it the problem of OpportunityLineItem? What should it be?
@isTest (SeeAllData=true)
private class InvoiceManagerTest {
    
    private static Account testAccount;
    private static Opportunity testOpportunity;
    private static Opportunity testEmptyOpportunity;
    private static OpportunityLineItem testProduct1;
    private static OpportunityLineItem testProduct2;
    
    /*
    * init test data
    */
    private static void initData(){
        //init account
        testAccount=new Account(Name='TestAccount', BillingCountry='France');
        insert testAccount;
        //init opportunity
        testOpportunity=new Opportunity(Name='TestOpportunity',stageName='Test',CloseDate=System.today());
        testEmptyOpportunity=new Opportunity(Name='testEmptyOpportunity',stageName='Test',CloseDate=System.today());
        insert new List<Opportunity>{testEmptyOpportunity,testOpportunity};
        
        // create  products
        Product2 p1 = new Product2(Name='Test Product');
        Product2 p2 = new Product2(Name='Test Product');
        insert new List<Product2>{p1,p2};
             
        // Add product to standard pricebook
        // Id taken from Salesforce.com
        String standardPriceBookId ='';
        for(Pricebook2 p:[Select id From Pricebook2 p where isStandard=true limit 1]){
            standardPriceBookId=p.id;
        }
        //System.debug(standardPriceBookId+':'+p1.id);
        PricebookEntry pbe1 = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p1.Id,UnitPrice=2,isActive=true);
        insert pbe1;
        PricebookEntry pbe2 = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id,UnitPrice=2,isActive=true);
        insert pbe2;
        
        
        testProduct1 = new OpportunityLineItem(TotalPrice=2 ,Quantity=2 ,PriceBookEntryId=pbe1.Id, OpportunityId=testOpportunity.Id,Ready_to_Invoice__c=true,Invoice__c=null);
        testProduct2 = new OpportunityLineItem(TotalPrice=2,Quantity=2,PriceBookEntryId=pbe1.Id, OpportunityId=testOpportunity.Id,Ready_to_Invoice__c=true,Invoice__c=null);
        insert new List<OpportunityLineItem>{testProduct1,testProduct2};
       
        
    }
    
    /** 
    *   Test method : testGenerateInvoice method covers the logic and expected results
    *   @category Testing Method
    */
    public static testMethod void testGenerateInvoice(){
        initData();
        test.startTest();
          String result1=InvoiceManager.generateInvoice(testEmptyOpportunity.id);
          System.assertNOTEquals(result1,testEmptyOpportunity.id);
          String result2=InvoiceManager.generateInvoice(testOpportunity.id);
        
          System.assertEquals(result2,testOpportunity.id);
          //check invoice
          List<Invoice__c> lstInvoice=[select id, Invoice_Date__c  from Invoice__c where Opportunity__c=:testOpportunity.id];
          System.assertEquals(lstInvoice.isEmpty(),false);
          List<OpportunityLineItem> lstProduct=[select Opportunity.Billing_Entity__C,Ready_to_Invoice__c,Invoice_Date__c,Invoice__c from OpportunityLineItem where id=:testProduct1.id OR id=:testProduct2.id];  
          for(OpportunityLineItem item:lstProduct){
            System.assertEquals(item.Invoice_Date__c,System.today());
            System.assertEquals(item.Invoice__c,lstInvoice.get(0).id);
          }
          
       test.stopTest();     
              
     }   

}

 
Best Answer chosen by Cherry Shen 1
cloudSavvyProgcloudSavvyProg
Hi Cherry,

It is possible scenerio that some validations were created directly in production org after the code was push from sandbox.

So the test is failing due to that reason. 

Ideal scenerio could be to change the test to update the opportunity as 'Won' first and then mark product as 'ready to invoice' and deploy to production.

Or trun off the validation rules in production for deploy.

Hope it helps.

Regards,
CloudSavvyProg

All Answers

cloudSavvyProgcloudSavvyProg
Hi Cherry,

It is possible scenerio that some validations were created directly in production org after the code was push from sandbox.

So the test is failing due to that reason. 

Ideal scenerio could be to change the test to update the opportunity as 'Won' first and then mark product as 'ready to invoice' and deploy to production.

Or trun off the validation rules in production for deploy.

Hope it helps.

Regards,
CloudSavvyProg
This was selected as the best answer
Cherry Shen 1Cherry Shen 1
Hi Rupa, Thanks for your quick reply, I get it now. Thank you again. Best Regards, Cherry
cloudSavvyProgcloudSavvyProg
Hi Cherry, 

If my post has helped you, can you please mark it as solved?
This will help to close the thread.

Regards,
CloudSavvyProd