I have a apex method which retrieves url parameters and perform SOQL and then DML operation. Here's the apex method,
public void updateObjRecord(){
try{
String objId = System.currentPageReference().getParameters().get('lid');
object__c objToUpdate = [Select Id, Name, Participated__c From object__c Where Id = :objId];
if (objToUpdate != null){
objToUpdate.Participated__c = true;
upsert objToUpdate;
}
this.showSuccess = true;
}catch(exception ex){
System.debug('Error connecting to the Lead: '+ex.getMessage());
this.errorMsg = 'There are some issues updating the Obj. Please use the form instead!';
this.showError = true;
}
}
I have written a test method to cover the above code and it works fine. However I wanted to write another test method for another user with different profile to execute the same code system.runas(). While it works well for normal method, it gives me List has no rows error on above leadToUpdate SOQL for this different user. Here is my test method,
@isTest
static void processObjTestMethod_withCounselor(){
String uniqueUserName = 'user' + DateTime.now().getTime() + '@dom.com';
Profile p = [SELECT Id FROM Profile WHERE Name='Counsellor'];
User u = new User(Alias = 'standt', Email='[email protected]',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName=uniqueUserName);
System.runAs(u) {
Test.startTest();
Object__c ld = [Select Id, Name, Mobile__c, Email__c, Participated__c from Object__c LIMIT 1];
ControllerClass ctrl = new ControllerClass();
PageReference pageRef = Page.vfPage;
test.setCurrentPageReference(pageRef);
pageRef.getParameters().put('lid',ld.Id);
ctrl.updateLeadRecord();
Test.stopTest();
}
Object__c objRec = [Select Id, Name, Participated__c From Object__c LIMIT 1];
System.debug('objRec ::'+objRec.Participated__c);
System.assertEquals(objRec.Participated__c, true);
}
Please note that I have a testsetup method to create Object__c record at the first place. Here's the setupdata method,
@testSetup static void setupTestData() {
Object__c ld = new Object__c();
ld.Email__c = '[email protected]';
ld.Mobile__c = '123456789';
ld.First_Name__c = 'fName';
ld.Last_Name__c = 'lName';
insert ld;
}
Where this could go wrong?
Object__c
org wide sharing setting marked asPrivate
? If so, the records created in testSetup are likely not visible to the running user. How are the records from testSetup being shared to this user?