1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Apex Specialist Super Badge 復習[Challenge 4]

Last updated at Posted at 2019-09-16

自動化処理のテスト

Challenge4-6はこれまでに作成したコードのテストケースの作成。
今回のはコールアウトするわけでもないのでMockなども不要です。

要件

  • トリガーのテスト:MaintenanceRequest(実質呼び出しているMaintenanceRequestHelper)
  • ポジティブなユースケース(正常系)
    • 300のメンテナンスリクエスト
    • vehicle,equipmentの関連、期日などが正しく作成されている
  • ネガティブなユースケース(異常系)
    • レコードが作成されない

ソースコード

正直見せるほどのものでもないので見ないほうがいい

コード
@isTest
public class MaintenanceRequestTest {
    
    static Vehicle__c vehicle;
    static Product2 equipment;
    static Product2 replacementParts;
    static void setup(){
        Database.SaveResult vResult=Database.insert(new Vehicle__c());
        Product2[] equipments=new Product2[]{new Product2(Name='test equip',Maintenance_Cycle__c=30),
            								new Product2(Name='replacement',Maintenance_Cycle__c=10,Replacement_Part__c=true)};
        Database.SaveResult[] eResult=Database.insert(equipments);
        vehicle=new Vehicle__c(Id=vResult.getId());
        equipment=new Product2(Id=eResult[0].getId());
        replacementParts=new Product2(Id=eResult[1].getId());
    }
    
    static Integer size=300;
    @isTest static void positiveTestCase_MaintenanceRequest(){
        setup();
        List<Case> listCase=createCaseList(size,'Repair');
        Id[] caseid=new Id[size];
        Integer index=0;
        for(Database.SaveResult sr:Database.insert(listCase)){
            caseid[index++]=sr.getId();
        }
        Work_Part__c expected=insertWorkParts(listCase.get(0).Id);
        for(Case c:listCase){
            c.Status='Closed';
        }
        Test.startTest();
        Database.SaveResult[] result=Database.update(listCase);
        Test.stopTest();
        Work_Part__c actual=[SELECT Equipment__c,Maintenance_Request__c
                             FROM Work_Part__c
                             WHERE Id=:expected.Id];
        System.assertNotEquals(expected, actual);
    }
    
    
    @isTest static void negativeTestCase_MaintenanceRequest(){
        setup();
        //incorrect type
        Case c=createCaseList(1,'Electrical').get(0);
        Database.SaveResult sr=Database.insert(c);
        Work_Part__c expected=insertWorkParts(sr.getId());
        c.Id=sr.getId();
        c.Status='Closed';
        Test.startTest();
        Database.SaveResult result=Database.update(c);
        Test.stopTest();
        Work_Part__c actual=[SELECT Equipment__c,Maintenance_Request__c
                             FROM Work_Part__c
                             WHERE Id=:expected.Id];
        System.assertEquals(expected, actual);
    }
    
    static List<Case> createCaseList(Integer count,String type){
        List<Case> result=new List<Case>();
        for(Integer i=0;i<count;i++){
            Case c=new Case();
            c.Type=type;
            c.Status='New';
            c.Vehicle__c=vehicle.Id;
            c.Subject='Test Repair'+i;
            c.Equipment__c=equipment.Id;
            result.add(c);
        }
        return result;
    }
    
    static Work_Part__c insertWorkParts(Id caseId){
        Work_Part__c workpart=new Work_Part__c(Equipment__c=replacementParts.Id,
                                               Maintenance_Request__c=caseId);
        workpart.Id=Database.insert(workpart).getId();
        return workpart;
    }
}

続き:Apex Specialist Super Badge 復習[Challenge 5]

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?