function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jenny jadejenny jade 

Test classes failing

Hi 

My test classes are failing and I donno what fix needsto be done .I dont understand code and because of which my production push is not happening.

Can anyone pleae help me  
Matt GarnettMatt Garnett
If you're writing Apex you need to be able to validate your code with test classes. This is a requirement. A general rule of thumb for coding in salesforce should be, if you can't write the test classes needed for your code try and can't succeed using declaritive methods, avoid it all together. Especially if trying to push to prod. 

For posting on forums you should always post your code snippet for other users to view.
jenny jadejenny jade
@isTest public class ContactIndustryTest { static void createTestData(Integer numAccounts, Integer numContacts, Boolean assignContactsToFirst) { Integer i; // ACCOUNTS i = 0; List accounts = New List(); while (i < numAccounts) { accounts.add(new Account(Name='Apex Test ' + i, Industry=null)); i++; } insert accounts; accounts = [SELECT Id, Name, Industry FROM Account]; system.assertEquals(numAccounts, accounts.size()); // CONTACT i = 0; Integer accountIndex; List contacts = New List(); while (i < numContacts) { if (assignContactsToFirst == true) { accountIndex = 0; } else { accountIndex = i; } contacts.add(New Contact(AccountId=accounts[accountIndex].Id, FirstName = 'Test', LastName='Test', Contact_Industry__c=null,LeadSource=null)); i++; } insert contacts; contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c,LeadSource FROM Contact]; system.assertEquals(numContacts, contacts.size()); } // when contact is created or its industry changes, update the contact industry based on the account industry @isTest static void inheritAccountIndustry() { // populate test data createTestData(2, 1, true); // pull test data List accounts = [SELECT Id, Name, Industry FROM Account]; List contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c,LeadSource FROM Contact]; // populate account with industry accounts[1].Industry = 'test'; update accounts; accounts = [SELECT Id, Name, Industry FROM Account]; // change contact to different account contacts[0].AccountId = accounts[1].Id; update contacts; contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact]; // industry should match system.assertEquals(accounts[1].Industry, contacts[0].Contact_Industry__c); } // when account industry changes, update contacts to match industry @isTest static void updateContactIndustry() { // populate test data createTestData(2, 1, true); // pull test data List accounts = [SELECT Id, Name, Industry FROM Account]; List contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact]; // populate account with industry accounts[0].Industry = 'test'; update accounts; accounts = [SELECT Id, Name, Industry FROM Account]; contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact WHERE AccountId = :accounts[0].Id]; // industry should match for (Contact c: contacts) { system.assertEquals(accounts[0].Industry, contacts[0].Contact_Industry__c); } } // when account industry changes, update contacts to match industry @isTest static void massUpdateContactIndustry() { // populate test data createTestData(250, 250, false); // pull test data List accounts = [SELECT Id, Name, Industry FROM Account]; List contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact]; // populate account with industry Integer i = 0; while (i < accounts.size()) { accounts[i].Industry = 'Test ' + i; i++; } update accounts; accounts = [SELECT Id, Name, Industry FROM Account]; contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact WHERE AccountId = :accounts[0].Id]; // industry should match i = 0; for (Contact c: contacts) { system.assertEquals(accounts[i].Industry, contacts[i].Contact_Industry__c); i++; } } }
jenny jadejenny jade
I copy pasted the code ,I donno why it got copied  without line numbers :(
jenny jadejenny jade
@isTest
public class ContactIndustryTest {

    static void createTestData(Integer numAccounts, Integer numContacts, Boolean assignContactsToFirst) {
        
        Integer i;
        
        // ACCOUNTS
        
            i = 0;
            List<Account> accounts = New List<Account>();
            
            while (i < numAccounts) {
                accounts.add(new Account(Name='Apex Test ' + i, Industry=null));
                i++;
            }
            
            insert accounts;    
            
            accounts = [SELECT Id, Name, Industry FROM Account];
            system.assertEquals(numAccounts, accounts.size());
                
        // CONTACT
        
            i = 0;
            Integer accountIndex;
            
            List<Contact> contacts = New List<Contact>();
    
            while (i < numContacts) {
            
                if (assignContactsToFirst == true) {
                    accountIndex = 0;
                } else {
                    accountIndex = i;
                }
            
                contacts.add(New Contact(AccountId=accounts[accountIndex].Id, FirstName = 'Test', LastName='Test', Contact_Industry__c=null,LeadSource=null));
                i++;
            }
            
            insert contacts;
            
            contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c,LeadSource FROM Contact];
            system.assertEquals(numContacts, contacts.size());

    }

    // when contact is created or its industry changes, update the contact industry based on the account industry
    @isTest static void inheritAccountIndustry() {
        
        // populate test data
        createTestData(2, 1, true);
        
        // pull test data
        List<Account> accounts = [SELECT Id, Name, Industry FROM Account];
        List<Contact> contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c,LeadSource  FROM Contact];
        
        // populate account with industry
        accounts[1].Industry = 'test';
        update accounts;
        
        accounts = [SELECT Id, Name, Industry FROM Account];
        
        // change contact to different account
        contacts[0].AccountId = accounts[1].Id;
        update contacts;
        
        contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact];
        
        // industry should match
        system.assertEquals(accounts[1].Industry, contacts[0].Contact_Industry__c);     
        
    }

    // when account industry changes, update contacts to match industry
    @isTest static void updateContactIndustry() {
        
        // populate test data
        createTestData(2, 1, true);
        
        // pull test data
        List<Account> accounts = [SELECT Id, Name, Industry FROM Account];
        List<Contact> contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact];
        
        // populate account with industry
        accounts[0].Industry = 'test';
        update accounts;
        
        accounts = [SELECT Id, Name, Industry FROM Account];
        
        contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact WHERE AccountId = :accounts[0].Id];
        
        // industry should match
        for (Contact c: contacts) {
            system.assertEquals(accounts[0].Industry, contacts[0].Contact_Industry__c);
        }
        
    }

    // when account industry changes, update contacts to match industry
    @isTest static void massUpdateContactIndustry() {
        
        // populate test data
        createTestData(250, 250, false);
        
        // pull test data
        List<Account> accounts = [SELECT Id, Name, Industry FROM Account];
        List<Contact> contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact];
        
        // populate account with industry
        Integer i = 0;
        while (i < accounts.size()) {
            accounts[i].Industry = 'Test ' + i;
            i++;
        }
        update accounts;
        
        accounts = [SELECT Id, Name, Industry FROM Account];
        
        contacts = [SELECT Id, FirstName, LastName, Contact_Industry__c FROM Contact WHERE AccountId = :accounts[0].Id];
        
        // industry should match
        i = 0;
        for (Contact c: contacts) {
            system.assertEquals(accounts[i].Industry, contacts[i].Contact_Industry__c);
            i++;
        }
        
    }

}

 
jenny jadejenny jade
User-added image
Matt GarnettMatt Garnett
Here you are defining LeadSource as null:
 
1contacts.add(New Contact(AccountId=accounts[accountIndex].Id, FirstName = 'Test',LastName='Test', Contact_Industry__c=null,LeadSource=null));

This is a common exception that stems from a validation exception. This error occurs when there is a validation rule and one of the test methods is hitting exception caused by a validation when creating/updating a test record.

The first step when solving a problem like this is to identify the field that is causing the exception. The field is named in the error message. Here it is LeadSource. In Salesforce, a less preferable way of tackling this problem is editing the validation rule to exclude the validation rule associated with LeadSource. The proper solution is to modify the record(s) in the DML operation such that they comply with the validation rule. The validation rule is there for a reason, so it should be obeyed. Turning off the validation rule temporarily isn't advisable, as it means the test method isn't reflective of the correct production schema, and hence neither is your Apex code.