I'm testing a controller for a Customer Community page where users can change their email address. To keep things simple for the user, I'm getting the controller to also change their username to match the email address.

When I ran my tests, I received an email saying that my username had changed. So, I boiled it down to a simple test case:

public class UsernameChangeTest {
    @isTest static void changeUserName() {
        update new User(Id = UserInfo.getUserId(), Username = 'not+' + UserInfo.getUserEmail());

If I run that as a test, I get an email saying that my username has changed. The username hasn't really changed - I guess it's been rolled back like test data always is.

So it looks like a bug in SF's sandboxing of tests? Anyone come across this before? Any workarounds? (other than horrible Test.isRunningTest() hacks to avoid certain lines of code)


If your unit tests are causing emails to send, open a case with support immediately. This behavior is a bug. Unit tests should not be able to cause actual email sends.

While you wait for support to resolve your case, you may need to pursue alternatives. If you are testing in a sandbox, at least, you can set Email Deliverability to No Access.

I haven't found any official documentation that unit tests should not be able to send emails, but it is widely acknowledged and has been for a long time. See, for example: How do I test an apex trigger that sends an email

I have opened a case with support (#15055094) in an attempt to report this finding as a bug.

UPDATE (11/10/2016) - Tier 3 has confirmed this is a bug but does not yet have an ETA to fix it.

  • This is going to sound really stupid, but where would I raise a case like this? If I go to raise a case about development, I just get the message "Developer support for standard customers and partners is supported directly through our community. If you have a developer support question, please click here" and I cannot raise it. It seems like you can only raise dev bugs if you're a higher-tier partner or a customer on premier support. – Aidan Nov 4 '16 at 15:30
  • That is exactly right, you need premier support. I'll see if I can repro and if so, I will raise a case myself. – Adrian Larson Nov 4 '16 at 15:32
  • Very odd. I originally saw the problem on e11. Just repro-ed on cs22. All I'm doing is pasting that exact code into the dev console and pushing the "Run Test" button. (Just to be clear, I'm not being really dumb and running this as anonymous apex) – Aidan Nov 4 '16 at 16:23
  • @AdrianLarson - In your testing, is it isolated to the User changes or do automated emails related to other issues like WFR, case notifications, etc also go out? And thanks for raising the case. – Eric Nov 4 '16 at 18:20
  • 1
    It seems to only affect Username changes. Even changing the Email does not exhibit the same behavior. – Adrian Larson Nov 4 '16 at 19:24

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.