2012-07-03 64 views
2

嗨,我想获得一个有几个未来的方法(用于Web服务调用)和几个具体的静态方法的类的覆盖率。但是在调用未来的方法之后,我无法调用其他方法...您能否告诉我如何覆盖未来的方法和Web服务调用。未来方法测试覆盖范围salesforce

我的阶级结构:

public with sharing class AccountSynchController { 
@future (callout=true) 
    public static void importAccount(Set<Id> ids) { 

    } 
@future (callout=true) 
    public static void importContact(Set<Id> ids) { 

    } 
} 

[future methods are called from trigger] 

Test Class Code : 


     VAT__c testVat = new VAT__c(); 
    testVat.Code__c = '3'; 
    insert testVat;   

     Account testAccount = new Account(); 
     testAccount.Name = 'Test Account'; 
     testAccount.VATSales__c = testVat.Id; 
     testAccount.VATPurchase__c = testVat.Id; 
     testAccount.KvK_Nummer__c = '12312312'; 
     testAccount.PhoneExt__c = '12312312'; 
     testAccount.Website = '12312312'; 
     testAccount.BillingPostalCode = '12312312'; 
     testAccount.BillingCity = '12312312'; 
     testAccount.Fax = '12312312'; 
     testAccount.Phone = '12312312'; 
     testAccount.BillingStreet = '12312312'; 
     testAccount.BTW_Nummer__c = '12312312'; 
     testAccount.BillingCountry = '12312312'; 
     testAccount.BillingState = '12312312'; 
     testAccount.BTW_Nummer__c = '12312312'; 
     testAccount.E_mail__c = '[email protected]'; 
     testAccount.Taal__c = 'NL'; 
     testAccount.SalesPaymentConditionCode__c = '15'; 
     testAccount.Code__c = '102'; 
     testAccount.fromExact__c = false; 
     testAccount.Exact_Id__c = '123123'; 
     insert testAccount; 

     Contact testContact = new Contact(); 
     testContact.AccountId = testAccount.Id; 
     testContact.Birthdate = system.today(); 
     testContact.Conact_Exact_Number__c = '12312312312'; 
     testContact.Email = '[email protected]'; 
     testContact.FirstName = 'first'; 
     testContact.Title_Code__c = 'Mr.'; 
     testContact.Geslacht__c = 'M'; 
     testContact.Initials__c = 'I'; 
     testContact.Language_Code__c = 'NL'; 
     testContact.LastName = 'last'; 
     testContact.MiddleName__c = 'middle'; 
     testContact.Phone = '12321312312'; 
     testContact.fromExact__c = false; 
     insert testContact; 

谢谢..

回答

4

致电Test.startTest()开始你的单元测试,然后运行测试插件。通过致电Test.stopTest()完成。调用最后一个方法可确保您的@future方法已被触发。之后,你可以做你的断言来验证触发器的行为。

+0

美丽,我的朋友,谢谢。这篇文章是在另一个阴天,充满Apex的日子里的一缕阳光。 –

+0

我认为,而不是Test.endTest(),它应该是Test.stopTest()。我没有看到endTest()方法。 http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_test.htm –

+0

@AdrianCarr好赶上,我编辑了答案。谢谢 –

1

扩展Adam的答案以测试标注,您将需要使用Test.isRunningTest()方法为您提供模拟从Web服务返回数据的机会 - 这不是最好的,但它是普遍接受的方式。

另一种选择是使用一些嘲讽和注射,但这并不像它应该是那么简单,所以大多数人都会选择第一种选择。

0

由于不可能直接从测试类发出web服务调用,所以必须编写一个成瘾模拟类。那是为了产生假的回应。

// This causes a fake response to be generated 
    Test.setMock(WebServiceMock.class, new WebServiceMockImpl()); 

其中WebServiceMock.class是上面提到的类。 之后,您可以在测试类中调用真正的webservice调出方法。

请查看以下链接了解更多信息。

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm

相关问题