1

I have a controller which I am trying to write a test class for:

//a controller class to get incidents for display on VF page and then 
//anonymise the incidents and related contacts
public class incidentAnonController {

    public Incident__c firstDate{get; set;}
    public Incident__c lastDate{get; set;}

    public incidentAnonController(ApexPages.StandardController controller){
        Integer dy = System.today().day();
        Integer mo = System.today().month();
        Integer yr = System.today().year();
        firstDate = new Incident__c(Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-6));
        lastDate = new Incident__c(Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-12));                        
}

    //method to anonymise relevant records
    public void anonymise() {
        //list of incidents older than 6 months. These will be anonymised.
        List<Incident__c> results = [SELECT Id, Incident_name__c, Date_of_incident__c, Name, Garda_Name__c
         FROM Incident__c WHERE Date_of_Incident__c <= :firstDate.Date_Of_Incident__c AND 
         Date_of_Incident__c >= :lastDate.Date_Of_Incident__c];


        //anonymise incidents that are older than 6 months
        for (Incident__c inc : results) {                    
            inc.Incident_name__c = inc.Name;
            inc.Garda_Name__c = '';
            inc.Pulse_ID__c = '';
        }                                   
        update results;

And here is my test class

@isTest 
private class incidentAnonTest {
    @isTest static void incidentAnon() {
        Integer dy = System.today().day();
        Integer mo = System.today().month();
        Integer yr = System.today().year();

        Incident__c inc1 = new Incident__c();

        inc1.Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-7);
        inc1.Incident_name__c = 'Incident 1';        
        inc1.Garda_Name__c = 'Test Garda';
        insert inc1; 

        ApexPages.StandardController sc = new ApexPages.StandardController(Inc1);
        incidentAnonController testInc = new incidentAnonController(sc);        
        testInc.anonymise();

        System.debug(inc1.Date_Of_Incident__c);
        System.debug(inc1.Garda_Name__c);
        System.assertEquals('', inc1.Garda_Name__c);

}    

What I expect to happen is the test to pass because Garda_Name__c has been cleared by anonymise(). However the test fails. I am not seeing any errors so I'm unsure why this is happening.

I have googled and looked for an answer but I can't come across anything because I don't know what to google since I have no error!

Thanks for any help

2

You need to re-query your data in order to see and make assertions about changes that have been committed.

inc1 = [SELECT Garda_Name__c FROM Incident__c WHERE Id = :inc1.Id];

Note that (via Primitive Data Types):

sObject String field values follow the same rules as in the SOAP API: they can never be empty (only null), and they can never include leading and trailing whitespace. These conventions are necessary for database storage.

It's often easiest to just make an assertion using String.isBlank() rather than a literal comparison, so you don't have to remember the difference or worry about whether the specific string value you're working with came from a query or was built in Apex, where a String can be ''.

  • The assertion would still fail, because the expected value is actually null. – Adrian Larson Jan 14 at 14:40
  • That's what I meant; I may not have expressed it clearly - I'd do System.assert(String.isBlank(inc1.Garda_Name__c), 'Garda Name anonymised'); No? – David Reed Jan 14 at 14:44
  • Thank you for your answer, that worked. What I did for the assert was System.assertEquals(String.isBlank(inc1.Garda_Name__c), TRUE); – Luke Bray Jan 14 at 14:46
  • @Luke That is definitely not the correct way to do it. Your expected value if you use that approach is true, not the method call. Personally, I would just use null, as you'll get clearer messages for equality assertion. – Adrian Larson Jan 14 at 14:50
  • @Adrian ok so just to be clear do you think I should still be using System.assertEquals(inc.Garda_Name__c, null);? The reason I have TRUE is because i thought I want my test to pass if ìnc1.Garda_Name__c is blank – Luke Bray Jan 14 at 15:05

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.