You need to sign in to do that
Don't have an account?
Sherwin Betonta
Catch block not covered on Test Class.
Can somebody help me with my code. I created a negative test for Helper Class to cover the catch block but it's still not successfully covered.
This is my helper class:
Here is my negative test method:
Thanks in advance.
This is my helper class:
public with sharing class AccOppRecalcHelperClass { //A method which will be called in the trigger public static void opportunityTriggerMethod(List<Opportunity> oppList, Map<Id,Opportunity> oldOpp){ //instantiate a list of ID's to store id's of account records List<id> accId = new List<id>(); //instantiate a list of accounts List<account> acc = new List<account>(); //initialize string variable closedWon to Closed Won stage field value stored in custom label String Closed_Won = Label.Closed_won; //initialize string variable closedLost to Closed Lost stage field value stored in custom label String Closed_Lost = Label.Closed_lost; String oldaccstat; String acctstat; Integer oppClosedLost = 0; Integer oppClosedWon = 0; //checks opportunity records from opportunity list for(Opportunity opp: oppList){ //checks if account related to opportunity field is not empty if(opp.AccountId != NULL){ //adds account ID related to opportunity record to list of ID's accId.add(opp.AccountId); } } for (Opportunity oppRec : [SELECT Id, Name, StageName FROM Opportunity WHERE AccountId IN :accId AND StageName != NULL]){ if( oppRec.StageName == Closed_Won){ oppClosedWon++; } else if (oppRec.StageName == Closed_Lost){ oppClosedLost++; } } for (Account myAccount : [SELECT ID, Number_of_Opportunities_Won__c, Number_of_Opportunities_Lost__c FROM Account WHERE ID in :accId ]){ myAccount.Number_of_Opportunities_Won__c = oppClosedWon; myAccount.Number_of_Opportunities_Lost__c = oppClosedLost; acc.add(myAccount); } if (acc.size()>0){ try{ update acc; } catch(Exception e){ System.debug(e); } } } }
Here is my negative test method:
@isTest private static void negativeTest(){ List<Account> accts = new List<Account>(); Account a = new Account(name= 'TestAccount1'); insert a; a.name = 'Sherwin'; Test.startTest(); accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE]; update a; Test.stopTest(); System.assert(accts.size() > 0, 'Was expecting to find at least one account'); System.assertEquals('Sherwin', a.name); }
Thanks in advance.
Then in your test class, have a test method with code similar to this:
System.NullPointerException: Attempt to de-reference a null object
test method: