You need to sign in to do that
Don't have an account?

APEX Test Method Error
I am trying to get better coverage of a particular controller extension and am having a few issues with one particular line. I have a custom object where I store a task Id and a user Id and a few other fields. This class is meant to query, insert, or delete records in that object.
In the getInformedUsers method below, all the lines test fine with the exception of the Return property. I keep getting a Attempt to dereference a null object error. The only other place where I can't seem to get coverage is in the removeInformed method for the redirect lines.
I am currently at 61% pass if I do not test the getInformedUsers method but would love figure out how to cover that as well. Code below:
public with sharing class informedUserList { private ApexPages.StandardController controller; public informedUserList(ApexPages.StandardController stdController) { controller = stdController; } public List<Task_Informed_Users__c> getInformedUsers() { String tskID = ApexPages.currentPage().getParameters().get('Id'); String longTaskId = String.valueOf(tskId); String shortTaskId = longTaskId.left(15); return [SELECT id, Informed_User__r.Name, Chatter_Notification__c, Send_Email__c FROM Task_Informed_Users__c WHERE Task_Id__c =: shortTaskId]; } public PageReference addInformed() { string tskId = ApexPages.currentPage().getParameters().get('Id'); PageReference tskInformed = new pagereference('/apex/informedUsers?tId='+tskId); tskInformed.setRedirect(true); return tskInformed; } public PageReference removeInformed() { string tskId = ApexPages.currentPage().getParameters().get('Id'); Id iuRecId = System.currentPageReference().getParameters().get('iuRec'); delete [select id from Task_Informed_Users__c where ID=: iuRecId ]; return null; PageReference tskInformed = new pagereference('/apex/taskDetail?Id='+tskId); tskInformed.setRedirect(true); return tskInformed; } public static Task testTsk; public static Task_Informed_Users__c testInformedUser; public static ID testTaskId; public static User testUserId; static testMethod void informedUserListTest() { Test.startTest(); testUserId = [SELECT ID FROM User WHERE LastName=: 'User2']; testTsk = new Task(Subject='testTask', ownerId=testUserId.Id); insert testTsk; testTaskId = testTsk.Id; testInformedUser = new Task_Informed_Users__c(Informed_user__c=testUserId.Id, Task_ID__c=testTaskId, Chatter_Notification__c=FALSE, Send_Email__c=FALSE); insert testInformedUser; Test.setCurrentPage(Page.taskDetail); ApexPages.currentPage().getParameters().put('tId',testTaskId); ApexPages.StandardController con = new ApexPages.StandardController(testInformedUser); informedUserList tInformedUserList = new informedUserList(con); tInformedUserList.addInformed(); //Tests Fine tInformedUserList.removeInformed(); //Tests all but the redirect code tInformedUserList.getInformedUsers(); //Get Attempt to DeReference a Null Object error Test.stopTest(); } }
Looks like a typo to me
references queryparm 'Id'
but your testmethod:
uses 'tId'
Hence your
fails as you can't do left() on null
All Answers
I think you need to change the order. You are removing the user and then searching for the remove user.
Swap these two lines and you should have it.
Looks like a typo to me
references queryparm 'Id'
but your testmethod:
uses 'tId'
Hence your
fails as you can't do left() on null
@arizona
I thought the same thing initially, but I tested removing the "remove" method from code as well as switching the order of execution in the test method and had the same result which seemed odd to me as well.
@crop1645
Yep, turned out to be a Typo... I wasn't looking at the parameter that I was pulling the ID from when I created the test method... I was looking at the name of the parameter I was passing to the URL.
Thanks for the extra set of eyes!