+ Start a Discussion
Tina Chang 6Tina Chang 6 

How to Write Test Class for Accounts with or without Opportunities?

Hello,  I wrote the following test class for my Apex code "LastModifiedOppty" but it didn't work with code coverage 0%.  Does anyone know how to fix this?  My apologies I have just started the coding portion of my Salesforce knowledge base, so any advice would be much appreciated!  Thank you!
@isTest
private class LastModifiedOpptyTest {
    
    @isTest static void createAccountWithOpp() {       
    // Accounts with Opportunities
        
    Account accts = new Account();
    accts.Name    = 'testAccount';
        
    Insert accts;
    
    Opportunity opp = new Opportunity();
    opp.Name        = 'testOpportunity';
    opp.StageName   = 'Prospecting';
    opp.CloseDate   = Date.today();
    opp.AccountId   = accts.Id;
    opp.Amount      = 100000;
        
    Insert opp; 

	Test.startTest();
    System.assertEquals(1, accts.Opportunities.Size()); 
    Test.stopTest();  
    
    }
    
    @isTest static void createAccountWithoutOpp() {
	// Accounts without Opportunities 

    Account accts2 = new Account();
    accts2.Name     = 'testAccount2';
        
    Insert accts2; 
        
    Test.startTest();
    System.assertEquals(0, accts2.Opportunities.Size()); 
    Test.stopTest(); 
    
  }
     
}
Here's my Apex class "LastModifiedOppty":
public class LastModifiedOppty {

    public static void updateLastOpptyIdField() {
      // Retrieve the last modified opportunity's ID for each account.
      Account[] accountList = [SELECT Id, (SELECT Id, Amount from Opportunities ORDER 
                               BY LastModifiedDate DESC LIMIT 1) FROM Account];
        
        
      // to access the opportunity related to individual account and pass the Id value on to the Last_Opportunity_ID__c field.
      // if the account does not have any opportunities, pass the value 'Without Oppty' to the Last_Opportunity_ID__c field.

		for(Account acct : accountList){

            if(acct.Opportunities.size()>0){
            Opportunity relatedOpp              = acct.Opportunities[0];                    
    			acct.Last_Opportunity_ID__c     = relatedOpp.Id;
                acct.Last_Opportunity_Amount__c = relatedOpp.Amount;
                                       
            } else if(acct.Opportunities.size()==0){
                acct.Last_Opportunity_ID__c     = 'Without Oppty';
            }
                
          	}
     
          update accountList;
      
	}
}


 
Best Answer chosen by Tina Chang 6
Tina Chang 6Tina Chang 6
Hello, I disabled two problematic triggers and revised the test class as follows to solve the System.AssertException issues and finally got 100% code coverage. Thank you.
 
@isTest
private class LastModifiedOpptyTest {
    
    @isTest static void testAccountWithOpp() {       
        // Accounts with Opportunities
            
        Account acct1   = new Account();
        acct1.Name      = 'testAccount1';
            
        Insert acct1;
        
        Opportunity opp = new Opportunity();
        opp.Name        = 'testOpportunity';
        opp.StageName   = 'Prospecting';
        opp.CloseDate   = Date.today();
        opp.AccountId   = acct1.Id;
        opp.Amount      = 100000;     
            
        Insert opp; 
    
            Test.startTest();           
        		LastModifiedOppty.updateLastOpptyIdField();       	
        	Test.stopTest();  
        
                List <Account> accountList = [SELECT Id, Last_Opportunity_Amount__c, Last_Opportunity_ID__c, 
                                             (SELECT Id, Amount from Opportunities ORDER BY LastModifiedDate DESC 
                                             LIMIT 1) FROM Account WHERE Id = :acct1.Id];

       			for(Account account : accountList){		
        		Opportunity relatedOpp = account.Opportunities[0];
                System.assertEquals(relatedOpp.Id, account.Last_Opportunity_ID__c);
                System.assertEquals(relatedOpp.Amount, account.Last_Opportunity_Amount__c);
        		}
     }
    
    @isTest static void testAccountWithoutOpp() {
        // Accounts without Opportunities 
    
        Account acct2 = new Account();
        acct2.Name    = 'testAccount2';
            
        Insert acct2; 
            
            Test.startTest();       	
        		LastModifiedOppty.updateLastOpptyIdField();        	
        	Test.stopTest(); 
                
                List <Account> accountList2 = [SELECT Id, Last_Opportunity_ID__c, (SELECT Id, Amount from 
                                              Opportunities ORDER BY LastModifiedDate DESC LIMIT 1) 
                                              FROM Account WHERE Id = :acct2.Id];
            	
                for(Account account2 : accountList2){		      		
                System.assertEquals('Without Oppty', account2.Last_Opportunity_ID__c); 
                } 
    }
     
}

 

All Answers

v varaprasadv varaprasad
Hi Tina,

Please check once below sample code : 
 
@isTest
private class LastModifiedOpptyTest {
    
    @isTest static void createAccountWithOpp() {       
    // Accounts with Opportunities
        
    Account accts = new Account();
    accts.Name    = 'testAccount';
        
    Insert accts;
    
    Opportunity opp = new Opportunity();
    opp.Name        = 'testOpportunity';
    opp.StageName   = 'Prospecting';
    opp.CloseDate   = Date.today();
    opp.AccountId   = accts.Id;
    opp.Amount      = 100000;
        
    Insert opp; 

	Test.startTest();
	 List<Account> accountList = [SELECT Id, (SELECT Id, Amount from Opportunities ORDER 
                               BY LastModifiedDate DESC LIMIT 1) FROM Account where id =: accts.id];
    System.assertEquals(1, accts.Opportunities.Size()); 
	LastModifiedOppty.updateLastOpptyIdField();
    Test.stopTest();  
    
    }
    
    @isTest static void createAccountWithoutOpp() {
	// Accounts without Opportunities 

    Account accts2 = new Account();
    accts2.Name     = 'testAccount2';
        
    Insert accts2; 
        
    Test.startTest();
    List<Account> accountList = [SELECT Id, (SELECT Id, Amount from Opportunities ORDER 
                               BY LastModifiedDate DESC LIMIT 1) FROM Account where id =: accts2.id];
    System.assertEquals(0, accts.Opportunities.Size()); 
	LastModifiedOppty.updateLastOpptyIdField();
    Test.stopTest(); 
    
  }
     
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Tina Chang 6Tina Chang 6
Hi, @Varaprasad,  thank you for the advice!  I edited the test class as follows but still am getting 0% code coverage.  Do you have any ideas why it is not working?  Thank you.

Test Result

Modified Test Class:
@isTest
private class LastModifiedOpptyTest {
    
    @isTest static void createAccountWithOpp() {       
    // Accounts with Opportunities
        
    Account acct1 = new Account();
    acct1.Name    = 'testAccount1';
        
    Insert acct1;
    
    Opportunity opp = new Opportunity();
    opp.Name        = 'testOpportunity';
    opp.StageName   = 'Prospecting';
    opp.CloseDate   = Date.today();
    opp.AccountId   = acct1.Id;
    opp.Amount      = 100000;
        
    Insert opp; 

	Test.startTest();
        
        List <Account> accountList = [SELECT Id, (SELECT Id, Amount from Opportunities ORDER 
                                                  BY LastModifiedDate DESC LIMIT 1) FROM Account WHERE Id = :acct1.Id];
        acct1.Last_Opportunity_ID__c     = opp.Id;
        acct1.Last_Opportunity_Amount__c = opp.Amount;
        System.assertEquals(1, acct1.Opportunities.Size());
    	LastModifiedOppty.updateLastOpptyIdField();
        
    Test.stopTest();  
    
    }
    
    @isTest static void createAccountWithoutOpp() {
	// Accounts without Opportunities 

    Account acct2 = new Account();
    acct2.Name    = 'testAccount2';
        
    Insert acct2; 
        
    Test.startTest();
    	
        List <Account> accountList2 = [SELECT Id, (SELECT Id, Amount from Opportunities ORDER 
                                                  BY LastModifiedDate DESC LIMIT 1) FROM Account WHERE Id = :acct2.Id];
        acct2.Last_Opportunity_ID__c = 'Without Oppty';
    	System.assertEquals(0, acct2.Opportunities.Size()); 
    	LastModifiedOppty.updateLastOpptyIdField();
    
    Test.stopTest(); 
    
  }
     
}
Tina Chang 6Tina Chang 6
Hello, I did some research and have further modified my test class as follows but still resulted in 0% coverage. I couldn't figure out why. Is there anyone that could help? Thank you!  :)
 
@isTest
private class LastModifiedOpptyTest {
    
    @isTest static void testAccountWithOpp() {       
        // Accounts with Opportunities
            
        Account acct1   = new Account();
        acct1.Name      = 'testAccount1';
            
        Insert acct1;
        
        Opportunity opp = new Opportunity();
        opp.Name        = 'testOpportunity';
        opp.StageName   = 'Prospecting';
        opp.CloseDate   = Date.today();
        opp.AccountId   = acct1.Id;
        opp.Amount      = 100000;     
            
        Insert opp; 
    
            Test.startTest();
                
                List <Account> accountList = [SELECT Id, (SELECT Id, Amount from Opportunities 
                                              ORDER BY LastModifiedDate DESC LIMIT 1) 
                                              ​FROM Account WHERE Id = :acct1.Id];
        
                System.assertEquals(opp.Id, acct1.Last_Opportunity_ID__c);
                System.assertEquals(opp.Amount, acct1.Last_Opportunity_Amount__c);
                
                LastModifiedOppty.updateLastOpptyIdField();
                
            Test.stopTest();  
    
    }
    
    @isTest static void testAccountWithoutOpp() {
        // Accounts without Opportunities 
    
        Account acct2 = new Account();
        acct2.Name    = 'testAccount2';
            
        Insert acct2; 
            
            Test.startTest();
                
                List <Account> accountList2 = [SELECT Id, (SELECT Id, Amount from Opportunities 
                                               ORDER By LastModifiedDate DESC LIMIT 1) 
                                               FROM Account WHERE Id = :acct2.Id];
            
                System.assertEquals('Without Oppty', acct2.Last_Opportunity_ID__c); 
                
                LastModifiedOppty.updateLastOpptyIdField();
            
            Test.stopTest(); 
    
  }
     
}

 
Tina Chang 6Tina Chang 6
Hello, I disabled two problematic triggers and revised the test class as follows to solve the System.AssertException issues and finally got 100% code coverage. Thank you.
 
@isTest
private class LastModifiedOpptyTest {
    
    @isTest static void testAccountWithOpp() {       
        // Accounts with Opportunities
            
        Account acct1   = new Account();
        acct1.Name      = 'testAccount1';
            
        Insert acct1;
        
        Opportunity opp = new Opportunity();
        opp.Name        = 'testOpportunity';
        opp.StageName   = 'Prospecting';
        opp.CloseDate   = Date.today();
        opp.AccountId   = acct1.Id;
        opp.Amount      = 100000;     
            
        Insert opp; 
    
            Test.startTest();           
        		LastModifiedOppty.updateLastOpptyIdField();       	
        	Test.stopTest();  
        
                List <Account> accountList = [SELECT Id, Last_Opportunity_Amount__c, Last_Opportunity_ID__c, 
                                             (SELECT Id, Amount from Opportunities ORDER BY LastModifiedDate DESC 
                                             LIMIT 1) FROM Account WHERE Id = :acct1.Id];

       			for(Account account : accountList){		
        		Opportunity relatedOpp = account.Opportunities[0];
                System.assertEquals(relatedOpp.Id, account.Last_Opportunity_ID__c);
                System.assertEquals(relatedOpp.Amount, account.Last_Opportunity_Amount__c);
        		}
     }
    
    @isTest static void testAccountWithoutOpp() {
        // Accounts without Opportunities 
    
        Account acct2 = new Account();
        acct2.Name    = 'testAccount2';
            
        Insert acct2; 
            
            Test.startTest();       	
        		LastModifiedOppty.updateLastOpptyIdField();        	
        	Test.stopTest(); 
                
                List <Account> accountList2 = [SELECT Id, Last_Opportunity_ID__c, (SELECT Id, Amount from 
                                              Opportunities ORDER BY LastModifiedDate DESC LIMIT 1) 
                                              FROM Account WHERE Id = :acct2.Id];
            	
                for(Account account2 : accountList2){		      		
                System.assertEquals('Without Oppty', account2.Last_Opportunity_ID__c); 
                } 
    }
     
}

 
This was selected as the best answer