2

Hi i trying to get coverage for a class that has few future method (for web service call) and few concrete static methods. but after calling future method, i am unable to call other methods... Can you please tell me how to take coverage for future method and web service calls.

my class structure :

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 = 'test@gmail.com';
        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 = 'test@gmail.com';
        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;

Thanks..

4

3 回答 3

5

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

于 2012-07-03T15:48:34.763 回答
2

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

另一种选择是使用一些模拟和注入,但这并不像它应该的那样直接,所以大多数人选择第一种选择。

于 2012-07-03T16:11:48.030 回答
0

由于不可能直接从您的测试类中调用 Web 服务,因此您必须编写一个成瘾模拟类。这样做的目的是生成虚假响应。

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

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

检查以下链接以获取更多信息。

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

于 2013-10-24T03:22:04.037 回答