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
111111 

can someone help me to test the Exception?

global class gg {
    WebService static Boolean updateDB(OpportunityLineItem[]  opportunityList) {

    Boolean updateFlg = true;
        Database.SaveResult[] SR = database.update(opportunityList,false);
        for(Database.SaveResult lsr:SR){
            if(!lsr.isSuccess()){
                    updateFlg = false;
                    break;
                }
            }          
    }
    return updateFlg;
}
 
     static testMethod void updateDB() {
    
          OpportunityLineItem[] opportunityList = null;
          opportunityList = [select mitosakuban__c from OpportunityLineItem];
          for (OpportunityLineItem opportunity : opportunityList) {
               
             opportunity.mitosakuban__c = '';
            
            }
           System.assertEquals(false, updateDB(opportunityList ));
    }
}
 
 
how can i make the method (database.update(opportunityList,false)) to be error,how can i test it ?
 
now my coverage can't  be 100%?can someone can give a testmethode?
Thankyou
Ron HessRon Hess
write a test method that constructs an opportunity line item, but do not give it a valid ID.

pass that into your method under test, it should fail in the update statement and throw an error.
111111
global class gg {
    WebService static Boolean updateDB(OpportunityLineItem opportunityList) {
    Boolean updateFlg = true;
    try{
              
        update opportunityList;
    }catch (Exception e){
            updateFlg = false;
    }
    return updateFlg;
}
 
 
     static testMethod void updateDB() {
 
        
          OpportunityLineItem oli = new OpportunityLineItem(opportunityid='fff',pricebookentryid='123', unitprice=1.5,quantity=2); 
           
           System.assertEquals(false, updateDB(oli ));
    }
}
 
 
i do not give it a valid ID [opportunityid='fff'],  but  " OpportunityLineItem oli = new OpportunityLineItem(opportunityid='fff',pricebookentryid='123', unitprice=1.5,quantity=2); 
"                   error  [System.StringException:Invalid id:fff] is appeared.
 
it is not   update-statement-error