@isTest private class FixtureSetDetailDailyUpdateBatchTest { static Product2 pro1; static Product2 pro2; static Fixture_Set_Detail__c fsd1; static void setupData(){ pro1 = new Product2(Name='name01',IsActive=true,Family='GI', Fixture_Model_No__c='SB-MODEL-01',Serial_Lot_No__c='S/N tracing', Fixture_Model_No_T__c = 'SB-MODEL-01', Asset_Model_No__c = 'Pro01', ProductCode_Ext__c='SB-PRO-CODE-01',Manual_Entry__c=false, ProductCode = 'SB-PRO-CODE-01'); pro2 = new Product2(Name='name02',IsActive=true,Family='GI', Fixture_Model_No__c='SB-MODEL-02',Serial_Lot_No__c='Lot tracing', Fixture_Model_No_T__c = 'SB-MODEL-02', Asset_Model_No__c = 'Pro02', ProductCode_Ext__c='SB-PRO-CODE-02',Manual_Entry__c=false, ProductCode = 'SB-PRO-CODE-02'); insert new Product2[] {pro1, pro2}; Fixture_Set__c fs = new Fixture_Set__c(); fs.Name = 'SB-FS-01'; fs.Fixture_Set_Body_Model_No__c = 'SB-FS-MODEL-01'; fs.Loaner_name__c = 'SB-FS-1'; insert fs ; fsd1 = new Fixture_Set_Detail__c(); fsd1.Name = 'SB-FSD-1'; fsd1.Name_CHN_Created__c = 'SB-中文FSD-1'; fsd1.Product2__c = pro1.Id; fsd1.Fixture_Set__c = fs.Id; fsd1.UniqueKey__c = fs.Id + ':' + pro1.Id; insert fsd1; } // 配套明细状态true->false static testMethod void testMethod1() { setupData(); List proList = [SELECT SFDA_Status__c FROM Product2]; System.assertEquals('停止',proList[0].SFDA_Status__c); System.assertEquals('停止',proList[1].SFDA_Status__c); Fixture_Set_Detail__c fsd = [SELECT Product_Status_Flag__c FROM Fixture_Set_Detail__c]; System.assertEquals(true, fsd.Product_Status_Flag__c); Test.startTest(); Database.executeBatch(new FixtureSetDetailDailyUpdateBatch(), 100); Test.stopTest(); fsd = [SELECT Product_Status_Flag__c FROM Fixture_Set_Detail__c]; System.assertEquals(true, fsd.Product_Status_Flag__c); } // 配套明细状态false->true static testMethod void testMethod2() { setupData(); pro1.SFDA_Status__c = '有效'; fsd1.Product_Status_Flag__c = false; update pro1; update fsd1; List proList = [SELECT SFDA_Status__c FROM Product2]; System.assertEquals('有效',proList[0].SFDA_Status__c); System.assertEquals('停止',proList[1].SFDA_Status__c); Fixture_Set_Detail__c fsd = [SELECT Product_Status_Flag__c FROM Fixture_Set_Detail__c]; System.assertEquals(false, fsd.Product_Status_Flag__c); Test.startTest(); Database.executeBatch(new FixtureSetDetailDailyUpdateBatch(), 100); Test.stopTest(); fsd = [SELECT Product_Status_Flag__c FROM Fixture_Set_Detail__c]; System.assertEquals(true, fsd.Product_Status_Flag__c); } static testMethod void testMethod3() { FixtureSetDetailDailyUpdateBatch b = new FixtureSetDetailDailyUpdateBatch(); b.errorList.add('Test'); Database.executeBatch(b); } }