• Sherwin Betonta
  • NEWBIE
  • 30 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
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:
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.
I'm to new to apex. 
I have a class that would allow Users from a public group (marketing group) to access records created by another user say "System Admin".

I wanted to create a test class that would create a record using the sys admin profile and check if the marketing group can have edit access to the record.

Here is my code: 
public class OpportunitySharing {
    //sharing record after creating
   public void shareAfterInsert(List<Opportunity> oppoList){
       OpportunityShare oppShare = new OpportunityShare();

       List<Group> MarketingGroup = [SELECT Id, Name FROM Group WHERE Name = 'Marketing Group'];

       for(Opportunity opp: oppoList) {
           for(Group mg : MarketingGroup) {
               
               oppShare.OpportunityId = opp.Id;
               oppShare.UserOrGroupId = mg.Id;
               oppShare.OpportunityAccessLevel = 'Edit';
               Database.SaveResult dbsr = Database.insert(oppShare, false);
           }
       }
   }
}