2015-08-08 95 views
2

我有以下服务:如何测试@Future方法

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类

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; 

    } 
} 

现在,当我尝试测试LibraryService去除方法,我不断收到以下错误:

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); 
    } 
} 

我尝试添加Test.startTest()和Test.stopTest()到testRemoveLibrary方法,但我仍然得到相同的错误。难道我做错了什么?我该如何解决?

+0

上面代码中的哪一行是抛出错误? –

回答

0
@isTest(SeeAllData=true) 
private class TestLibrary { 

    static testMethod void testRemoveLibrary() { 

     Library__c library = new Library__c(...); 
     Test.start(); 
     Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id)); 
     Test.stop(); 
     System.assertEquals(isRemoved, true); 
    } 
} 

请添加Test.start并停止包括您的方法。