3

I keep getting an assertion failure with my test class for the following trigger:

   trigger WebToCaseCountry on Case (after insert)
{

    Set<String> countries = new Set<String>();
    for (Case c : Trigger.new)
    {
        if (!String.isEmpty(c.CountryWebFormText__c) && c.Newsletter__c == TRUE)
        {
            countries.add(c.CountryWebFormText__c);
        }
    }

    if (countries.size() > 0)
    {
        Map<String, Id> filteredCountries = new Map<String, Id>();
        for (Area__c a : [select Name, Id from Area__c where Name in :countries])
        {
            filteredCountries.put(a.Name, a.Id);
        }
        if (filteredCountries.size() > 0)
        {
            List<Case> caseUpdates = new List <Case>();
            List<Contact> contactUpdates = new List <Contact>();
            for (Case c : Trigger.new)
            {
                if (filteredCountries.containsKey(c.CountryWebFormText__c))
                {
                    Id areaId = filteredCountries.get(c.CountryWebFormText__c);
                    caseUpdates.add(new Case(Id = c.Id, Country_of_Origin__c = areaId));
                    if (c.ContactId != null)
                    {
                        contactUpdates.add(new Contact(Id = c.ContactId, Country__c = areaId));
                    }
                }
            }
            update caseUpdates;
            update contactUpdates;
        }
    }
}

This is my test class:

@isTest(SeeAllData=true)
public class WebToCaseCountryTest
{

    public static testMethod void testTrigger()
    {
        Area__c ar = new Area__c(Name = 'CA');
    insert ar;

    Case caseObj = new Case(CountryWebFormText__c ='CA', Newsletter__c = TRUE);
    insert caseObj;
    System.debug(caseObj);

    System.assertEquals(caseObj.CountryWebFormText__c, caseObj.Country_of_Origin__c);

    }
}

This is the error I am getting:

System.AssertException: Assertion Failed: Expected: CA, Actual: null -- STACK TRACE: Class.WebToCaseCountryTest.testTrigger: line 19, column 1

Anyone know why this is happening?

  • If you can use System.debug in your code you would also be able to know how this is happening. – Ashwani Aug 20 '15 at 8:33
  • You have to query on CaseObj and get Country_of_Origin__c field. Also @isTest(SeeAllData=true) is a bad practise. Always keep it as false – Pranay Jaiswal Aug 20 '15 at 8:41
  • I have added Case result = [SELECT Country_of_Origin__c FROM Case WHERE Id = :caseObj.Id]; But the insert fails? – Nik Aug 20 '15 at 8:45
  • is insert failing or assert is? – Pranay Jaiswal Aug 20 '15 at 8:47
1

Try this

public static testMethod void testTrigger()
{
    Area__c ar = new Area__c(Name = 'CA');
insert ar;

Case caseObj = new Case(CountryWebFormText__c ='CA', Newsletter__c = TRUE);
insert caseObj;
System.debug(caseObj);
caseObj=[Select id,countryWebFormText__c,Country_of_Origin__r.name from Case where id=:caseObj.id limit 1];

System.assertEquals(caseObj.CountryWebFormText__c, caseObj.Country_of_Origin__r.name);

}

From what we know is Country_of_Origin__C is a lookup field of type Area__C . caseObj.Country_of_Origin__c will only contain the ID of Area__C record, while what you want is the name of Area__C record. So to get the Name you have to query it as Country_of_Origin__R.name get the name of inserted Area.

As commented , seeAllData as true is generally not recommended.

  • Thank you so much! I notice the Limit 1 and that actually was an error I was getting previously, that it was returning more than 1 and I wasn't sure why. But now it makes sense. This has been so helpful I am going to read up more on testing and triggers with multiple objects and I will definitely refer to this in future if I have any problems. Thanks again :) – Nik Aug 20 '15 at 9:14
  • 1
    Yeah.. keep good work flow.. Here is something for your reference on writing best test cases,,, salesforce.stackexchange.com/questions/8558/… – Pranay Jaiswal Aug 20 '15 at 10:03
1

In You logic you are checking for ContactID

if (c.ContactId != null)
{
    contactUpdates.add(new Contact(Id = c.ContactId, Country__c = areaId));
}

But in your test method you didn't even created a contact record. So how would above condition work without contact. It will always return false and nothing will be updated. That will cause assert failure.

  • Hi @Ashwani I am just in the process of creating a contact now, but I don't see how this would affect the Case.Country_of_Origin__c field? That should contain a value right? – Nik Aug 20 '15 at 8:41

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.