0

I have a test class which is testing a controller but I cannot get the test to pass.

Here is the controller class:

//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;

Here is my test class:

@isTest 
public class incidentAnonTest {

public static testMethod void incidentAnon() {
    Integer dy = System.today().day();
    Integer mo = System.today().month();
    Integer yr = System.today().year(); 

    //create an incident
    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; 

    PageReference pageRef = Page.AnonymiseIncidents;
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(inc1);
    incidentAnonController testInc = new incidentAnonController(sc);

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

    testInc.anonymise();

    System.debug(inc1.Date_Of_Incident__c);
    System.debug(inc1.Garda_Name__c);

    System.assertEquals(TRUE, String.isBlank(inc1.Garda_Name__c));        

}    

And my VF page:

<apex:page standardController="Incident__c" extensions="incidentAnonController" docType="html-5.0">
<apex:form > 

    <apex:pageBlock title="Notes on Usage">
        <apex:pageBlockSection >
            //some content
        </apex:pageBlockSection>
    </apex:pageBlock>

    <apex:pageBlock title="Date Ranges for Anonymisation">
        <apex:pageBlockSection >
            <apex:inputField value="{!firstDate.Date_Of_Incident__c}" label="From (Default 6 Months ago)"/>              
            <apex:inputField value="{!lastDate.Date_Of_Incident__c}" label="To (Default 1 year ago)"/>     
        </apex:pageBlockSection>
    </apex:pageBlock>

    <apex:pageBlock title="List of Incidents">
            <apex:pageBlockTable value="{!incidents}" var="inc">               
                <apex:column headerValue="Incident Number">
                    <apex:outputLink value="/{!inc.id}">{!inc.Name}</apex:outputLink>                    
                </apex:column>
                <apex:column value="{!inc.Incident_Name__c}"/>
                <apex:column value="{!inc.Date_Of_Incident__c}"/>
            </apex:pageBlockTable>

            <apex:pageBlockButtons >
                <apex:commandButton action="{!anonymise}" value="Anonymise"/>
                <apex:commandButton onclick="location.reload();" value="Update List"/>
            </apex:pageBlockButtons>

    </apex:pageBlock>                
</apex:form>

I have tried lots of solutions and at one point thought it was to with the fact I don't reference my VF page. I tried using pageReference but couldn't get that to work.

The code has no errors it just seems as if testInc.anonymise(); is doing nothing at all.

1

Its probably doing what it should do but you're querying records at wrong moment.

public static testMethod void incidentAnon() {
    Integer dy = System.today().day();
    Integer mo = System.today().month();
    Integer yr = System.today().year(); 

    //create an incident
    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; 

    PageReference pageRef = Page.AnonymiseIncidents;
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(inc1);
    incidentAnonController testInc = new incidentAnonController(sc);

    Test.startTest();
    testInc.anonymise();
    Test.stopTest();

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

    System.debug(inc1.Date_Of_Incident__c);
    System.debug(inc1.Garda_Name__c);

    System.assertEquals(TRUE, String.isBlank(inc1.Garda_Name__c));        

}  

You need to query your data after doing actions on that data. Also its better to wrap tested logic in the Test.startTest(); and Test.stopTest(); so you can also test how your part of the code is behaving against Salesforce Limits

  • Yes thank you, that worked. Glad it was such a simple solution. I will mark as answered when allowed. Thank you again. – Luke Bray Jan 18 '19 at 13:09

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.