4

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:

@istest
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)

2

1 Answer 1

7

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.

6
  • 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
    Commented Nov 4, 2016 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
    Commented Nov 4, 2016 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
    Commented Nov 4, 2016 at 16:23
  • 1
    It seems to only affect Username changes. Even changing the Email does not exhibit the same behavior.
    – Adrian Larson
    Commented Nov 4, 2016 at 19:24
  • 1
    seems like there is a new KI for this same behavior, 5 years later... it is marked as no fix...
    – gorav
    Commented Feb 18, 2021 at 21:56

You must log in to answer this question.

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