1

I am receiving the following error message when trying to deploy a couple of unrelated classes and triggers. Does anybody have advice for me on how I can pass it, or should I get in touch with our contractors?

System.AssertException: Assertion Failed: Expected: 200, Actual: 400 Stack Trace: Class.AccountTriggerTest.updateStatus_hasCDFpermission_statusUpdatedCreatesCDFupdateMessagesAndSheduledJobs: line 83, column 1

BELOW IS MY THE TEST TRIGGER CLASS

@isTest
private class AccountTriggerTest {

private static final Integer BULK_SIZE = 200;

// Batch size for Account name changes is set in AccountTrigger - only one batch job can be created in a test
private static final Integer ACCOUNT_BATCH_SIZE = 100;

private static final String CDF_RESYNC_ACTION_INSERT = 'I';
private static final String CDF_RESYNC_ACTION_UPDATE = 'U';
private static final String CDF_RESYNC_ACTION_DELETE = 'D';

@testSetup
public static void insertWSPassword() {
    CDF_Integration__c CDFParams = TestDataUtilities.setCustomSetting();
}

private static List<Account> createTestAccounts(Integer accountsToCreate) {

    List<Account> testAccounts = new List<Account>();

    for(Integer i = 0; i < accountsToCreate; i++) {
        testAccounts.Add(new Account(name = 'Test Account ' + i, Status__c = 'Active'));
    }

    insert testAccounts;

    List<CDF_Resync_Job__c> newCDFMessages = [SELECT Id FROM CDF_Resync_Job__c WHERE Action__c = :CDF_RESYNC_ACTION_INSERT];
    System.assertEquals(accountsToCreate, newCDFMessages.size());

    return testAccounts;
}

@isTest
private static void updateStatus_noCDFpermission_errorsCreatedNoCDFmessagesNoNewScheduledJobs() {
    List<Account> testAccounts = createTestAccounts(BULK_SIZE);

    for(Account thisAccount :testAccounts) {
        thisAccount.Status__c = 'Inactive';
        thisAccount.CDF_Verified_Update__c = false; // shouldn't be necessary, as default is false
    }

    Test.startTest();

        try {
            update testAccounts;
            System.assert(false);
        } catch(DMLexception e) {
            System.assertEquals(true, e.getMessage().contains(String.format(Label.No_CDF_Verification, new List<String>{'Status'})));
        }

    Test.stopTest();

    List<CDF_Resync_Job__c> newCDFMessages = [SELECT Id FROM CDF_Resync_Job__c WHERE Action__c = :CDF_RESYNC_ACTION_UPDATE];
    System.assertEquals(0, newCDFMessages.size());

    // One scheduled CDF Resync Job is already on the queue from when the accounts were created
    List<AsyncApexJob> scheduledJobs = [SELECT Status FROM AsyncApexJob];
    System.assertEquals(1, scheduledJobs.size());
}

@isTest
private static void updateStatus_hasCDFpermission_statusUpdatedCreatesCDFupdateMessagesAndSheduledJobs() {
    List<Account> testAccounts = createTestAccounts(BULK_SIZE);

    for(Account thisAccount :testAccounts) {
        thisAccount.Status__c = 'Inactive';
        thisAccount.CDF_Verified_Update__c = true;
    }

    Test.startTest();
        update testAccounts;
    Test.stopTest();

    testAccounts = [SELECT Status__c, CDF_Verified_Update__c FROM Account];

    for(Account thisAccount :testAccounts) {
        System.assertEquals('Inactive', thisAccount.Status__c);
        System.assertEquals(false, thisAccount.CDF_Verified_Update__c);
    }

    List<CDF_Resync_Job__c> newCDFMessages = [SELECT Id FROM CDF_Resync_Job__c WHERE Action__c = :CDF_RESYNC_ACTION_UPDATE];
    System.assertEquals(BULK_SIZE, newCDFMessages.size());

    // One scheduled CDF Resync Job is already on the queue from when the accounts were created,
    // another is created after the update
    List<AsyncApexJob> scheduledJobs = [SELECT Status FROM AsyncApexJob];
    System.assertEquals(2, scheduledJobs.size());
}

@isTest
private static void updateName_nameChangeAddedToProcessingBatch_nameUpdatedCreatesCDFupdateMessagesAndSheduledJobs() {
    List<Account> testAccounts = createTestAccounts(ACCOUNT_BATCH_SIZE);

    for(Account thisAccount :testAccounts) {
        thisAccount.name = thisAccount.name + ' new';
    }

    Test.setMock(WebServiceMock.class, new CdfHttpMocks.WebServiceMockImpl());

    Test.startTest();
        update testAccounts;
    Test.stopTest();

    testAccounts = [SELECT Name FROM Account];

    for(Account thisAccount :testAccounts) {
        System.assertEquals(true, thisAccount.Name.contains('new'));
    }

    List<CDF_Resync_Job__c> newCDFMessages = [SELECT Id FROM CDF_Resync_Job__c WHERE Action__c = :CDF_RESYNC_ACTION_UPDATE];
    System.assertEquals(ACCOUNT_BATCH_SIZE, newCDFMessages.size());

    // One scheduled CDF Resync Job is already on the queue from when the accounts were created,
    // another CDF Resync Job is created after the update
    // and one AccountNameProcessingBatch job is created for the name changes
    List<AsyncApexJob> scheduledJobs = [SELECT Status FROM AsyncApexJob];
    System.assertEquals(3, scheduledJobs.size()); // one job when accounts are created, another after update
}

@isTest
private static void deleteAccounts_noErrors_createsCDFdeleteMessagesNoNewScheduledJobs() {
    List<Account> testAccounts = createTestAccounts(BULK_SIZE);

    Test.startTest();
        delete testAccounts;
    Test.stopTest();

    testAccounts = [SELECT Id FROM Account];
    System.assertEquals(0, testAccounts.size());

    List<CDF_Resync_Job__c> newCDFMessages = [SELECT Id FROM CDF_Resync_Job__c WHERE Action__c = :CDF_RESYNC_ACTION_DELETE];
    System.assertEquals(BULK_SIZE, newCDFMessages.size());

    // One scheduled CDF Resync Job is already on the queue from when the accounts were created,
    // another CDF Resync Job is created after the update
    // (there are no callouts when deleting Accounts via the API)
    List<AsyncApexJob> scheduledJobs = [SELECT Status FROM AsyncApexJob];
    System.assertEquals(2, scheduledJobs.size());
  }

}

CDF RESYNC JOB BELOW

trigger CDFResyncJobTrigger on CDF_Resync_Job__c (after insert) {

    if(trigger.isAfter && trigger.isInsert) {

        if(Test.isRunningTest()) {
            CDFintegration testCDFIntegration = new CDFIntegration(new CdfHttpMocks.CDFHttpServiceMock());
            new ResyncUtilities(trigger.newMap, testCDFIntegration).processResync();
        } else {
            new ResyncUtilities(trigger.newMap).processResync();
        }

    }
}
  • 2
    Can you please post your code? It looks like you have an assertion that is falling. "System.assert". This is probably because the outcome is 400 and not 200 what it is what the assert was expecting, but we need the code. – Carlos Naranjo May 26 '16 at 17:27
  • does the test code help, or do you need the classes triggers I'm trying to move up to production? – Glenn Daly May 26 '16 at 17:32
  • CDF_Resync_Job__c is looking like a custom setting because i'm not seeing anywhere you are creating records of it.So probably in your org you have around 400 records of that.If that is true you need to probably update Bulk_Size to 400 or limit query to retrieve only 200 records. – Mr.Frodo May 26 '16 at 17:36
  • The code line giving the problem is this one: "System.assertEquals(BULK_SIZE, newCDFMessages.size());" I think that a long your code there is a place in which you are probably adding up to 400 records to the newCDFMessages. It may be a good idea to insert some system.debug(newCDFMessages.size()) to get indications on where you are reaching that amount. That can help you find out why as well. – Carlos Naranjo May 26 '16 at 17:41
  • As Triggers and Order of Execution explains, a workflow rule can cause a trigger to run a second time which might be causing double counting. The test is signalling the bug; turning off the workflow rule might both allow the deployment and avoid the bug with real data until you get the trigger fixed. – Keith C May 26 '16 at 17:46
1

Unless you have access to read the Class AccountTriggerTest you will have to contact your contractor.

The error indicates a problem in a unit test. The expected result 200 (a count or a number field?) returned 400. Best guess is something ran twice. Based on the actual being twice the expected.

  • My guess would be that a workflow field update caused a trigger to run twice, and that trigger caused twice as many records to be created, thus breaking the test. It's a pretty common problem, and indicates a design flaw with the code in question. – sfdcfox May 27 '16 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.