I have a piece of code which upserts some objects. My code is robust enough to handle any DML failures. But i want to test it out in Unit tests.

Is there a way i can simulate DML failures?

General functioning of code - //Create a Master Object1. Upsert it //Create multiple Detail record under Object1, and upsert them

My code makes sure that if Master Object1 didn't get upserted (due to any DML failure), its handled well and it wont get to upserting Detail. Or if a single detail object record fails, everything gets rolled back and the exception is handled well. I want to test that functionality in unit test.

How can i do that? In manual test, i generally make a field required, then let the code run, and then revert the field back to not required. Can something like this be simulated in unit test?

  • 1
    There are ways to test failure to save and assert failure to save, but it would help to see what your code is doing, and how you are handling the failure. Can you share some code excerpts to give us an idea of how this test could be done? – pchittum Apr 15 '15 at 7:14

My way in this situations - using Service Locator

public class MyServiceLocator {

    private static final Map<Type, Type> customTypesMap = new Map<Type, Type> {
        MyIDatabase.class => MyDatabase.class,

        // Services
        MyICustomObjectService.class => MyCustomObjectService.class,
        MyIHttpService.class => MyHttpService.class,

        // DAOs
        MyICustomObjectDao.class => MyCustomObjectDao.class

    private static final Map<Type, Type> testTypesMap = new Map<Type, Type> {
        // Mocks
        MyICustomObjectDao.class => MyTestCustomObjectDao.class,
        MyIHttpService.class = MyTestHttpService.class

    public static Type resolve(Type t) {
        if (Test.isRunningTest()) {
            if (testTypesMap.containsKey(t)) {
                return testTypesMap.get(t);

        if (customTypesMap.containsKey(t)) {
            return customTypesMap.get(t);

        return t;


And in MyTestCustomObjectDao I can throw exceptions. For different behaviors you can add flag for unit test (throw or not throw exceptions).


Not sure whether there is a better way of doing this but what I did is usually just manually throw an exception in my unit test code.


You should you following code in your main class:

if(Test.isRunningTest() && Throw_Exception == True){ 
System.debug('This is not working properly'); 
// Throw your exception

In your test class set Throw_Exception = True and then call your method which will throw the exception.


Adding code will be tough. It's a huge code. This is what I am doing in short -

There is a queue (which is basically records of a custom object). Each record is an orderItem's Id.

I have a batch process that runs periodically and goes through queue. It takes them off the queue and tries to process those orderItems.

And by "process them", I mean it takes orderItems, goes to corresponding account, and under respective service contract, creates Contract Line Item.

If it can't upsert those contract line items (and this is where I need to simulate that upsert for few contract line items failed while succeeded for others), it adds their account Id into a different queue.

So let's say there were 100 order items, my batch process creates a list of 100 contract line item and tries to upsert them (I allow partial success). I want to test that for failed upserts, whether their account Id is added to another queue or not. So I want to simulate, say for example, 10-15 of those 100 upserts to fail, so I can test the whole functionality.

Thanks guys.

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.