1

I've the following services:

public with sharing class LibraryService {

    public static void remove(String jsonString) {
        Library__c library = [ SELECT Id, ilms__Library_Name__c FROM ilms__Library__c WHERE Id = libraryId ] ;

        AccessService.deleteReviewerGroup(library);

        delete library;
    }
}

AccessService class

public  with sharing class AccessService {
        public static void deleteLibraryReviewerGroup(Library__c library) {

        List<Library__Share> reviewersGroups = [ SELECT UserOrGroupId FROM ilms__Library__Share WHERE AccessLevel = 'Read' AND ParentId = :library.Id  ];
        System.debug('reviewersGroups: ' + reviewersGroups);

        if(reviewersGroups.size() == 1) {
            String reviewersGroupId = reviewersGroups[0].UserOrGroupId;

            delete reviewersGroups;

            AccessService.deleteReviewerGroup(reviewersGroupId);
        }

        return;
    }
    @future
    public static void deleteReviewerGroup(String groupId) {
        List<Group> reviewerGroup = [ SELECT Id FROM Group WHERE Id = :groupId ];

        delete reviewerGroup;

    }
}

Now, when I try to test the LibraryService remove method: I keep receiving the below error: first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa):

@isTest(SeeAllData=true)
private class TestLibrary {
    static testMethod void testRemoveLibrary() {
        Library__c library = new Library__c(...);
        Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id));

        System.assertEquals(isRemoved, true);
    }
}

I tried adding Test.startTest() and Test.stopTest() to the testRemoveLibrary method, but I still get the same error. Am I doing something wrong? How to fix this.

  • @laceysnr, I'm not sure what made you think that Question has been already answered, I've tried all the ways that are mentioned and with no luck I posted another one here ... – Surya Chandra Rao Gandreddi Aug 11 '15 at 4:18
  • I marked it as a duplicate because it's essentially the same question. One of the primary goals of SFSE is avoid the kind of duplication you see on systems like forums. If you've tried those techniques without success then update your code and question here to reflect that and we'll see if anyone has any further insights. – Matt Lacey Aug 11 '15 at 23:46
3

You can use System.runAs() to avoid issues with working on both setup and non-setup objects during tests. The easiest way to leverage it is with your current user (surprising, but it works). Give this a try:

@isTest(SeeAllData=true)
private class TestLibrary {
    static testMethod void testRemoveLibrary() {
        Library__c library = new Library__c(...);
        User u = [select Id from User where Id = : UserInfo.getUserId()];

        System.runAs(u)
        {
            Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id));
            System.assertEquals(isRemoved, true);
        }
    }
}
  • Thanks for the response, but I still get the same error: Delete failed. First exception on row 0 with id 00Gj00000015hzNEAQ; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Group, original object: ilms__Library__c: [] – Surya Chandra Rao Gandreddi Aug 9 '15 at 14:26
  • My bad, this approach works when you need to do both in your test method itself. Re-reading your code I think it's because you're deleting the Group and the Share at once. Have you run this code successfully outside of the tests? – Matt Lacey Aug 9 '15 at 23:31
  • Yes, it is working fine. when I delete a library. – Surya Chandra Rao Gandreddi Aug 10 '15 at 3:43
  • Yes, it is working fine (outside of the test), but it fails in the Test Method. – Surya Chandra Rao Gandreddi Aug 10 '15 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.