• Alex Sidlinskiy
  • NEWBIE
  • 35 Points
  • Member since 2015
  • Email Marketing Specialist

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 6
    Replies
I have a small problem. I'm trying to run the code snipper provided in the TrailHead for apex testing on "Creating TEst Data for Apex Tests" and my test code fails two test cases out 4. Here what my test runs results.

User-added image

Here what the code looks like:
@isTest
private class TestAccountDeletion {

    @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create one account with one opportunity by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(1,1);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(accts[0], false);
        Test.stopTest();

        // Verify that the deletion should have been stopped by the trigger,
        // so check 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());
    }
    
    @isTest static void TestDeleteAccountWithNoOpportunities() {
        // Test data setup
        // Create one account with no opportunities by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(1,0);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(accts[0], false);
        Test.stopTest();

        // Verify that the deletion was successful
        System.assert(result.isSuccess());
    }
    
    @isTest static void TestDeleteBulkAccountsWithOneOpportunity() {
        // Test data setup
        // Create accounts with one opportunity each by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(200,1);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult[] results = Database.delete(accts, false);
        Test.stopTest();

        // Verify for each record.
        // In this case the deletion should have been stopped by the trigger,
        // so check that we got back an error.
        for(Database.DeleteResult dr : results) {
            System.assert(!dr.isSuccess());
            System.assert(dr.getErrors().size() > 0);
            System.assertEquals('Cannot delete account with related opportunities.',
                                 dr.getErrors()[0].getMessage());
        }
    }
    
    @isTest static void TestDeleteBulkAccountsWithNoOpportunities() {
        // Test data setup
        // Create accounts with no opportunities by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(200,0);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult[] results = Database.delete(accts, false);
        Test.stopTest();

        // For each record, verify that the deletion was successful
        for(Database.DeleteResult dr : results) {
            System.assert(dr.isSuccess());
        }
    }
}

Any help much appreciated.
I'm writting a unit test on apex snippet that restricts contact by name or last name to be specific. I'm getting error "Invalid type: database.InsertResults" on line 18. Thanks in advance.
 
@isTest
public class TestRestrictContactByName {
 	
    @isTest static void TestInvalidName() {
        // Test data setup
        // Create empty list for contacts
        List<Contact> contacts = new List<Contact>{};
        
        //Iterate over each contact
        for(Integer i=0; i<200; i++){
            Contact ct = new Contact(LastName='INVALIDNAME' + i);
            contacts.add(ct);
        }
        
        // Perform test
        Test.startTest();
        insert contacts;
       	Database.InsertResults result = Database.insert(contacts, false);
        Test.stopTest();

        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() == 200);
        System.assertEquals('The Last Name "'+contacts.LastName+'" is not allowed for DML',
                             result.getErrors()[0].getMessage());
    }	
}

 
I'm getting "Return value must be of type: Contact". Not clear on how to address it. Any help much appreciated.
public class ContactSearch{
    
	//static seartchForContacts method of type Contact
    public static Contact searchForContacts(String inputlastName, String inputPostalCode) {

    	String lastNameToBeMatched = inputlastName;
    	String postalCodeToBeMatched = inputPostalCode;

        //Check whather user input varialbes empty or not
    	if(lastNameToBeMatched != null && postalCodeToBeMatched != null){
	    	//Return matched contacts in a list of type Contact
	    	List<Contact> matchedContacts = [SELECT Account.Name FROM Contact 
	                 WHERE LastName =:lastNameToBeMatched AND MailingPostalCode=:postalCodeToBeMatched];
			return matchedContacts ;//ERROR IS ON THIS LINE

		}else{

			return null;
		}
    }
}

 
I have a small problem. I'm trying to run the code snipper provided in the TrailHead for apex testing on "Creating TEst Data for Apex Tests" and my test code fails two test cases out 4. Here what my test runs results.

User-added image

Here what the code looks like:
@isTest
private class TestAccountDeletion {

    @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create one account with one opportunity by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(1,1);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(accts[0], false);
        Test.stopTest();

        // Verify that the deletion should have been stopped by the trigger,
        // so check 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());
    }
    
    @isTest static void TestDeleteAccountWithNoOpportunities() {
        // Test data setup
        // Create one account with no opportunities by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(1,0);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(accts[0], false);
        Test.stopTest();

        // Verify that the deletion was successful
        System.assert(result.isSuccess());
    }
    
    @isTest static void TestDeleteBulkAccountsWithOneOpportunity() {
        // Test data setup
        // Create accounts with one opportunity each by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(200,1);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult[] results = Database.delete(accts, false);
        Test.stopTest();

        // Verify for each record.
        // In this case the deletion should have been stopped by the trigger,
        // so check that we got back an error.
        for(Database.DeleteResult dr : results) {
            System.assert(!dr.isSuccess());
            System.assert(dr.getErrors().size() > 0);
            System.assertEquals('Cannot delete account with related opportunities.',
                                 dr.getErrors()[0].getMessage());
        }
    }
    
    @isTest static void TestDeleteBulkAccountsWithNoOpportunities() {
        // Test data setup
        // Create accounts with no opportunities by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(200,0);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult[] results = Database.delete(accts, false);
        Test.stopTest();

        // For each record, verify that the deletion was successful
        for(Database.DeleteResult dr : results) {
            System.assert(dr.isSuccess());
        }
    }
}

Any help much appreciated.
I'm writting a unit test on apex snippet that restricts contact by name or last name to be specific. I'm getting error "Invalid type: database.InsertResults" on line 18. Thanks in advance.
 
@isTest
public class TestRestrictContactByName {
 	
    @isTest static void TestInvalidName() {
        // Test data setup
        // Create empty list for contacts
        List<Contact> contacts = new List<Contact>{};
        
        //Iterate over each contact
        for(Integer i=0; i<200; i++){
            Contact ct = new Contact(LastName='INVALIDNAME' + i);
            contacts.add(ct);
        }
        
        // Perform test
        Test.startTest();
        insert contacts;
       	Database.InsertResults result = Database.insert(contacts, false);
        Test.stopTest();

        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() == 200);
        System.assertEquals('The Last Name "'+contacts.LastName+'" is not allowed for DML',
                             result.getErrors()[0].getMessage());
    }	
}