0

Trying to test a function in a class. The function in the class is the following :

public static boolean clearPosition(Id recId) {
  YRec__c rec = [Select Id, Position__c From YRec__c];
  rec.Position__c = null;
  update rec;
  return true;
}

And this is my test function :

public static void checkClearPosition() { 
 YRec__c obj = new YRec__c();
 obj.Position__c = '12843449l49';
 insert obj;

 Boolean result = clearPosition(obj.Id);
 //The result I get is true
 System.debug(obj.Position__c); //Prints out the number I set above instead of null
}

Why isn't my object updated? I've inserted it and then in the function I call, the record gets updated but now when I access the field in the function it still shows the initial value.

1 Answer 1

4

Because you are changing the instance whose reference is inside your function, after being queried by the SOQL on the first line.

If you want to verify that the value has changed, you'll have to query your record again before the assertion. That, or pass your object as the argument, and return it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .