You need to sign in to do that
Don't have an account?
Dave Shulman
Error in Batch Apex Test Class Help
Hi All,
Im writing a test class for batch apex that will be scheduled to run on the first of each month. Were testing it here, but i keep running into this error at line 25 "System.QueryException: List has no rows for assignment to SObject". I dont know why that list would be blank. Can anyone advise? thanks/
Im writing a test class for batch apex that will be scheduled to run on the first of each month. Were testing it here, but i keep running into this error at line 25 "System.QueryException: List has no rows for assignment to SObject". I dont know why that list would be blank. Can anyone advise? thanks/
public class TouchSalesDatasTest { @isTest public static void Test() { Map<ID,Sales_Data__c> SDMap = new Map<ID,Sales_Data__c>(); Account Acc = TestDataFactory.createAccount('TouchSalesDataTest EU ', 'desiredrecordtypeid', null, null, true); Account Dist = TestDataFactory.createAccount('TouchSalesDataTest Dist ', 'desiredrecordtypeid', null, null, true); for (Integer i = 0; i < 200; i++) { Sales_Data__c SD = new Sales_Data__c(End_User__c = Acc.ID, Distributor__c = Dist.Id, Amount__c = i, Quantity__c = i, Date__c = System.today().addDays(-25), Product__c = 'desiredproductid'); SDMap.put(SD.Id,SD); } insert SDMap.values(); Decimal expectedValue = System.today().addDays(-25).daysBetween(System.today())/31; Test.startTest(); TouchSalesDatas t = new TouchSalesDatas(); Database.executeBatch(t); Test.stopTest(); Sales_Data__c UpdatedTest = [SELECT ID,Name,Months_Ago__c FROM Sales_Data__c WHERE ID IN :SDMap.keySet() Limit 1]; System.assertEquals(Math.round(expectedValue), UpdatedTest.Months_Ago__c); } }
You cannot have ID, till the time you insert it. Your line no - 14 , should not have any Id.
Change it as below: (I'm doing for account)
Map<id,Account> oldmap = new Map<id,Account>();
Account acct = new Account(Name='Test Account',phone='123456789');
insert acct;
oldmap.put(acct.id, acct); // adding Id and Details.
P.S. - Make sure you put different ID for different records as in MAP we cannot have same keys (here key is ID)
Hope this helps!
Thanks,
Sucharita
All Answers
You cannot have ID, till the time you insert it. Your line no - 14 , should not have any Id.
Change it as below: (I'm doing for account)
Map<id,Account> oldmap = new Map<id,Account>();
Account acct = new Account(Name='Test Account',phone='123456789');
insert acct;
oldmap.put(acct.id, acct); // adding Id and Details.
P.S. - Make sure you put different ID for different records as in MAP we cannot have same keys (here key is ID)
Hope this helps!
Thanks,
Sucharita