Toplu tepe noktasını yürüten zamanlanabilir bir sınıfın test edilmesi tutarsız sonuçlar verir
@isTest(seeAllData = false)
private class interfacetest {
public static testMethod void testInterfaceScheduler(){
Product2 testproduct = new Product2(Name='test product',ProductCode = '112233');
insert testproduct;
Test.startTest();
InterfaceCalloutMock fakeResponse = new InterfaceCalloutMock(200);
Test.setMock(HttpCalloutMock.class, fakeResponse);
InterfaceSchedule InterfaceSc = new InterfaceSchedule();
String sch = '0 0 23 * * ?';
system.schedule('Test Interface Scheduler', sch, InterfaceSc);
Test.stopTest();
List<Product_Image__c> images = [select Id from Product_Image__c where Product__c =:testproduct.Id];
system.assertEquals(1, images.size());
}
public static testMethod void testInterfaceBatch(){
Product2 testproduct = new Product2(Name='test product',ProductCode = '112233');
insert testproduct;
Test.startTest();
InterfaceCalloutMock fakeResponse = new InterfaceCalloutMock(200);
Test.setMock(HttpCalloutMock.class, fakeResponse);
InterfaceBatch Batchtest = new InterfaceBatch();
database.executebatch(Batchtest,100);
Test.stopTest();
List<Product_Image__c> images = [select Id from Product_Image__c where Product__c =:testproduct.Id];
system.assertEquals(1, images.size());
}
public class InterfaceCalloutMock implements HttpCalloutMock {
Integer responseCode {get;set;}
InterfaceCalloutMock(Integer responseCode){
this.responseCode = responseCode;
}
public HTTPResponse respond(HTTPRequest req) {
HttpResponse resp = new HttpResponse();
resp.setStatusCode(responseCode);
resp.setBody('[{"id":"31A3FCE7-DDEF-40D1-8365A8CAA8809348"}]');
return resp;
}
}
}
Programlanabilir sınıf, yürütme yöntemindeki toplu iş ucunu çalıştırır.
Bazı nedenlerden dolayı, testInterfaceScheduler onaylama işlemi BAŞARISIZ, testInterfaceBatch ise onaylamayı GEÇTİ. Nedenini anlayamıyorum, herhangi bir yardım için minnettarım.
Teşekkür ederim!
Yanıtlar
Bunun nedeni, testInterfaceScheduler
sonra iki eşzamansız işlemi yürütmeye çalışmanızdır.Test.stoptest()
- Programlanabilir
- Toplu
Test.stopTest (), assert deyimine devam etmeden önce yalnızca ilk zaman uyumsuz işlemi yürütür. Hata ayıklama günlüğünüz çalıştırılan toplu dosyayı gösterecek, ancak gerçekte (test bağlamında) onaylamadan sonra gerçekleşir.
Şimdi, bunu düzeltmenin bir yolu, uygun birim testi yapmaktır.
- bir programlanabilirliği test ettiğinizde; yapıcısını ve execute () yöntemini test etmeniz yeterlidir. Gerçekten önemsediğiniz şey, execute () 'nin bir toplu işlem başlatmasıdır. Ve toplu işlem için bir AsyncApexJob olduğunu doğrulayabilirsiniz. Partinin başladığını test etmek zorunda değilsiniz.
- Bir batchable sınarken için sahte nesneleri sağlamak
start()
bulmak için, daha sonra doğrulamakexecute()
vefinish()
ne isterseniz yapın
Yapıcı bağımsız değişkenlerini toplu işlenebilire aktarıyorsanız, toplu işlenebilirin uygun bağımsız değişkenlerle çağrıldığını şu şekilde doğrulayabilirsiniz:
@IsTest static void testBatchable() {
InterfaceSchedule schedulable = new InterfaceSchedulable(args);
Test.startTest();
schedulable.execute(null); // SchedulableContext can't be constructed
Test.stoptest(); // batchable will execute now
// do asserts
}
Planlanabilir execute()
böyle görünür
public void execute(SchedulableContext sc) {
InterfaceBatch batchable = new InterfaceBatch(this.args); // from Dependency-injected constructor args
Database.executeBatch(batchable);
}
Yani, toplu işlenebilirliğe geçirmek istediğiniz bazı bağımsız değişkenleri programlanabilir içine enjekte edersiniz. Bu, programlanabilir ürününüz için bir args yapıcısına ve "args içeren" bir kurucuya sahip olacağınız anlamına gelir