function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dave ShulmanDave 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/
 
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);
    }
}

 
Best Answer chosen by Dave Shulman
SUCHARITA MONDALSUCHARITA MONDAL
Hi Dave,
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

SUCHARITA MONDALSUCHARITA MONDAL
Hi Dave,
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

 
This was selected as the best answer
Dave ShulmanDave Shulman
Thanks for the tip my friend, that did the trick
David Zhu 🔥David Zhu 🔥
You may try this:
 
public class TouchSalesDatasTest {
	@isTest
    public static void Test() {
        List<ID> SDIds= new List<ID>();
        List<Sales_Data__c > SDs= new List<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');
         SDs.add(SD);
        }
        insert SDs;

        for (Sales_Data__c  sd : sds)
        {
             SDIds.add(sd.Id);
         }
        
        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 :SDids Limit 1];
        System.assertEquals(Math.round(expectedValue), UpdatedTest.Months_Ago__c);
    }
}