You need to sign in to do that
Don't have an account?
slane
Not able to get System.runAs to work correctly
Hello all:
I am writing a test method that exercises code that duplicates a parent object and its related children. I'm trying to exercise the part of the code that traps for errors inserting the new records. Here's the code I want to exercise
My strategy was simply to use System.runAs to run as user with insufficient permissions to insert these records. I create a new user on the fly with the appropriate profile, use System.runAs to "become" that user, and run the duplicate routine, all from my test class. I'd expect this to cause the Database.Insert command to return a null set, in which case I should see an exception, which should be caught and managed by this test code:
But no exception is thrown. To all appearances, the insert succeeds. I have verified that the relevant profile does not have create privs on either object. I'm using the "with sharing" keyword on both the test class and the class being tested.
Any ideas as to why the active profile doesn't seem to prevent my test from creating records?
Thanks,
Steve Lane
I am writing a test method that exercises code that duplicates a parent object and its related children. I'm trying to exercise the part of the code that traps for errors inserting the new records. Here's the code I want to exercise
Code:
Savepoint sp = Database.setSavepoint(); Database.SaveResult sr = Database.Insert (a, false); String err; String newRebateID = sr.getID(); if ( !sr.isSuccess() ) { Database.rollback(sp); err = 'Failed to duplicate Rebate: '+this.rebateToClone.id; System.debug(err); Throw new SMART_Exception(err); }
My strategy was simply to use System.runAs to run as user with insufficient permissions to insert these records. I create a new user on the fly with the appropriate profile, use System.runAs to "become" that user, and run the duplicate routine, all from my test class. I'd expect this to cause the Database.Insert command to return a null set, in which case I should see an exception, which should be caught and managed by this test code:
Code:
system.runas( paragoUser ) { Exception theException = null; try { clonedRebateId = duplicator.dupRebate(); } catch ( Exception e ) { theException = e; } // these will throw NPE if the dupe succeeds, which it should not to pass test System.assertEquals( theException.getTypeName(), 'SMART_Exception' ); System.assertEquals( theException.getMessage(), 'Failed to duplicate Rebate: '+ theReb.Id ); }
But no exception is thrown. To all appearances, the insert succeeds. I have verified that the relevant profile does not have create privs on either object. I'm using the "with sharing" keyword on both the test class and the class being tested.
Any ideas as to why the active profile doesn't seem to prevent my test from creating records?
Thanks,
Steve Lane
All Answers
Thanks,
Steve