0

I want a test class like:

if system.runAs(user) {
    Test.startTest();

    delete new List<Sobject>(); // should give error.

    Test.stopTest();
}

How can I do this in my test class?

2
5

I usually prefer to use the following pattern when you need an all-or-none test:

try {
    delete records;
    System.assert(false, "Expected DMLException");
} catch(DMLException e) {
    // Validate specific errors here
}
2
  • Thanks Sfdcfox...so in expected DMLException what should we give..the full error which we are getting or something else Jun 22 '16 at 17:43
  • @DavidMycka See Exception Class and Built-in Exceptions in the developer's guide for usage of DMLException (it's near the bottom of the document). You can verify specific error texts, which field(s) are associated with the error, etc.
    – sfdcfox
    Jun 22 '16 at 17:46

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.