Another Salesforce blog!!!

Salesforce, Apex

writing test classes in apex


private static List<User> agentUsers;
private static List<Profile> agentProfiles;

agentProfiles = [select id from Profile where Name LIKE '%Agent%' limit 1];

agentUsers = TestDataUtil.createUsers(1, true, new Map<string, object>{
                                                                        'FirstName' => 'JOHN',                 
                                                                        'Email' => 'test3@test.com',                 
                                                                        'emailencodingkey' => 'UTF-8',                 
                                                                        'languagelocalekey' => 'en_US',                 
                                                                        'localesidkey' => 'en_US',
                                                                        'Username' => 'agent@test.com',
                                                                        'timezonesidkey' => 'America/Los_Angeles',
                                                                        'CommunityNickname' => 'agentCommNickName',
                                                                        'profileId' => agentProfiles[0].Id } );



public static List<User> createUsers(Integer numToInsert, Boolean doInsert, Map<String, Object> nameValue) 
{
	List<User> users = new List<User>();
	for( Integer i = 0; i < numToInsert; i++ ) {
		User u = new User(LastName = 'test' + String.valueOf(i), Alias = 'atest' + String.valueOf(i));
		for(String key : nameValue.keySet()){
		   u.put(key, nameValue.get(key));
		}
		u.put('Username', nameValue.get('Username') + String.valueOf(i)); 
		u.put('CommunityNickname', nameValue.get('CommunityNickname') + String.valueOf(i)); 
		users.add(u);
	}
	if( doInsert ) {
	  insert users;
	}
	return users;
}

System.runAs(agentUsers[0]){


}
@isTest
private class TestAccountDeletion {

    @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
        Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=acct.Id);
        insert opp;
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(acct, false);
        Test.stopTest();

        // Verify 
        // In this case the deletion should have been stopped by the trigger,
        // so verify that we got back an error.
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('Cannot delete account with related opportunities.',
                             result.getErrors()[0].getMessage());
    }
    
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*