I insert lists that are created from pre-existing data.

In my @testSetup I create the mockup but it's 'impossible' to enter the DMLException when trying to achieve 100% code coverage.

This is my code + code coverage: enter image description here

The only solution I found, and that works, is to update my code by creating a new 'artificial' Method to do the insert, and that would be easy to test entering the DMLException.

-> new method : enter image description here

Change in the code : enter image description here

And in test class : enter image description here

It works but isn't there a better way of doing it?

I'm really not found of the idea of add artificial coding to production code, just to get a better coverage...

  • Also if you don't want to be down-voted for nothing, remove the word bull**** from the post ;) Oct 25, 2023 at 14:02
  • 1
    Ohw... I forgot I choose that name, I should have called it dummyClass or whatsoever, you're right Oct 25, 2023 at 14:51
  • 3
    Posting code screenshots rather than text fragments is also a reason for attracting downvotes. Please take a moment to read How to Ask and take the tour.
    – identigral
    Oct 25, 2023 at 16:45
  • Agreed, it's a shame @identigral, but it's for a superBadge and Salesforce hate to see parts of those code posted... Oct 25, 2023 at 17:49
  • Also, this question was more a conceptual one, in my opinion. Not a code particuliar issue, more a general one ! Oct 25, 2023 at 17:50

1 Answer 1


You can try set up invalid lookup key for the case in your @testSetup. For example:

insert new Case(OwnerId = '005000000000000');

This will give you INVALID_CROSS_REFERENCE_KEY. You can use this mocked userId (key prefix is OK, but the id is actually non-existent).

You must log in to answer this question.

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