@isTest private class NFM605BatchTest { @testSetup static void makeTestRepair() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { throw new ControllerUtil.myException('not found 病院 recodetype'); } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype'); } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { throw new ControllerUtil.myException('not found 診療科 消化科 recodetype'); } Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM105TestCompany'; upsert company; Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id]; Account depart = new Account(); depart.RecordTypeId = rectDpt[0].Id; depart.Name = '*'; depart.Department_Name__c = 'NFM105TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; Maintenance_Contract__c contract = new Maintenance_Contract__c(); contract.Department__c = depart.Id; insert contract; // 截止日期是昨天 NewMaintenanceReport_Task__c report = new NewMaintenanceReport_Task__c(); report.Name = 'test'; report.NewMaintenance_Contract__c = contract.Id; report.reissueBatch__c = true; report.CorrespondingPeriod__c = Date.today().addDays(-1); insert report; // 截止日期是7天前 NewMaintenanceReport_Task__c report2 = new NewMaintenanceReport_Task__c(); report2.Name = 'test'; report2.NewMaintenance_Contract__c = contract.Id; report2.reissueBatch__c = true; report2.CorrespondingPeriod__c = Date.today().addDays(-7); insert report2; NewMaintenanceReport_Task__c report3 = new NewMaintenanceReport_Task__c(); report3.Name = 'test'; report3.NewMaintenance_Contract__c = contract.Id; report3.reissueBatch__c = true; report3.CorrespondingPeriod__c = Date.today().addDays(-30); insert report3; } @IsTest static void myTest1(){ Test.startTest(); // 查找指定天数的 Database.executeBatch(new NFM605Batch(NFMUtil.formatDate2Str(Date.today().addDays(-7)))); Test.stopTest(); } @IsTest static void myTest(){ Test.startTest(); // 默认查找昨天的 Database.executeBatch(new NFM605Batch()); Test.stopTest(); } @IsTest static void myTest2(){ Test.startTest(); // 查找指定数据 List < Id > idList = new List < Id >(); List getList = [Select Id from NewMaintenanceReport_Task__c]; for(NewMaintenanceReport_Task__c task:[Select Id from NewMaintenanceReport_Task__c]){ idList.add(task.Id); } if (idList.size() > 0) { Database.executeBatch(new NFM605Batch(idList)); } Test.stopTest(); } static testMethod void myTest3() { System.Test.startTest(); Database.executeBatch(new NFM605Batch(true),100); System.Test.stopTest(); } }