You need to sign in to do that
Don't have an account?
Steve Cairney
List out of bounds
Hi, I'm trying to load test data into a @isTest but I need to account for Lists and I can't find the right solution, the error is always that the list is out of bounds. I need to load test data for deployment into my org (in the sandbox I was using seeAllData)
The webservice class looks like this (jsut the GET section here)
And here's my test class, as you can see I've not faciliated any List data yet as I can't work out the best approach. If we use the first List as an example
How do I load test data with at least 1 record in the list?
@isTest here
The webservice class looks like this (jsut the GET section here)
webservice static String GetBookingDetails(ID id) { JSONGenerator jsonGenerator = JSON.createGenerator(true); ItsApproved_Booking__c booking = [SELECT Id, Name, Booking_Title__c, Customer_Reference__c, Incharge_Date__c FROM ItsApproved_Booking__c WHERE Id = :id]; jsonGenerator.writeStartObject(); // Booking //jsonGenerator.writeStringField('CompanyId', '798365d8-4278-4b16-a330-c4a3a9e1f91f'); // Staging Company Id //jsonGenerator.writeNumberField('UserId', 84); // Staging Company Id jsonGenerator.writeStringField('CompanyId', '042D7C6D-00E4-4C81-901A-BC8F8B545441'); // Live Company Id jsonGenerator.writeNumberField('UserId', 96); // Live User Id jsonGenerator.writeStringField('BookingTitle', booking.Booking_Title__c); jsonGenerator.writeStringField('CustomerReference', booking.Customer_Reference__c); jsonGenerator.writeStringField('CustomerBookingId', booking.Name); jsonGenerator.writeDateTimeField('InchargeDate', booking.Incharge_Date__c); List<ItsApproved_Product__c> products = [SELECT Id, IAProduct__c, Booking_Reference__c FROM ItsApproved_Product__c WHERE Booking_Reference__c = :Id]; jsonGenerator.writeFieldName('Products'); jsonGenerator.writeStartArray(); // Products for(ItsApproved_Product__c p: products) { List<IA_Product__c> IAProducts = [SELECT Id, Code__c FROM IA_Product__c WHERE Id = :p.IAProduct__c]; jsonGenerator.writeStartObject(); // Products jsonGenerator.writeStringField('ProductionCode', IAProducts[0].Code__c); jsonGenerator.writeStringField('CustomerProductReference', IAProducts[0].Code__c); List<ItsApproved_Design__c> designs = [SELECT Id, Artwork_Title__c FROM ItsApproved_Design__c WHERE Product_Reference__c = :p.Id]; jsonGenerator.writeFieldName('Designs'); jsonGenerator.writeStartArray(); // Designs for(ItsApproved_Design__c d: designs) { jsonGenerator.writeStartObject(); jsonGenerator.writeStringField('CustomerDesignId', d.Id); jsonGenerator.writeStringField('DesignName', d.Artwork_Title__c); List<ItsApproved_Delivery__c> deliveries = [SELECT Id, IADepot__c, Quantity__c FROM ItsApproved_Delivery__c WHERE ItsApproved_Design__c = :d.Id]; jsonGenerator.writeFieldName('Deliveries'); jsonGenerator.writeStartArray(); // Deliveries for(ItsApproved_Delivery__c del: deliveries) { List<IADepot__c> IADepots = [SELECT Id, Depot_Code__c FROM IADepot__c WHERE Id = :del.IADepot__c]; jsonGenerator.writeStartObject(); // Deliveries jsonGenerator.writeStringField('DeliveryId', del.Id); jsonGenerator.writeStringField('DepotCode', IADepots[0].Depot_Code__c); jsonGenerator.writeNumberField('Quantity', del.Quantity__c); jsonGenerator.writeEndObject(); // Deliveries } jsonGenerator.writeEndArray(); // Deliveries jsonGenerator.writeEndObject(); // Designs } jsonGenerator.writeEndArray(); // Designs jsonGenerator.writeEndObject(); // Products } jsonGenerator.writeEndArray(); // Products jsonGenerator.writeEndObject(); // Booking return jsonGenerator.getAsString(); }
And here's my test class, as you can see I've not faciliated any List data yet as I can't work out the best approach. If we use the first List as an example
List<ItsApproved_Product__c> products = [SELECT Id, IAProduct__c, Booking_Reference__c FROM ItsApproved_Product__c WHERE Booking_Reference__c = :Id];
How do I load test data with at least 1 record in the list?
@isTest here
@isTest public class Testing_ItsApprovedWebServices { @isTest public static void ItsApprovedWebServicesTest(){ ItsApproved_Booking__c bookingdetails = new ItsApproved_Booking__c(Booking_Title__c = 'Test', Customer_Reference__c = 'Test',Incharge_Date__c = System.today() ); insert bookingdetails; ItsApproved_Product__c productdetails = new ItsApproved_Product__c(Booking_Reference__c = bookingdetails.Id); insert productdetails; IA_Product__c iaproduct = new IA_Product__c(Code__c = '606'); insert iaproduct; ItsApproved_Design__c designdetails = new ItsApproved_Design__c(Product_Reference__c = productdetails.Id); insert designdetails; ItsApproved_Delivery__c deliverydetails = new ItsApproved_Delivery__c(Product__c = productdetails.Id, ItsApproved_Design__c = designdetails.Id); insert deliverydetails; Test.startTest(); Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); String username = ItsApprovedWebServices.Login('test'); String details = ItsApprovedWebservices.GetBookingDetails(bookingdetails.Id); Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator2()); String postbooking = ItsApprovedWebServices.PostBooking('{"test" : "test"}','authorization_id'); Test.stopTest(); } }
All Answers
I'm been spitballing and I'm wondering if this might be on the right track?
Weird thing is it's telling me there's a syntax error on line 5 around here: @isTest public static void ItsApprovedWebServicesTest() {
public void createTestRecords(Integer numProducts){
expecting a semi-colon, found '('
So I can't save it...
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm
I understand you can't hardcode the Id in because it does not exist in production - you are on the right track with creating the data and then passing the Id of the newly created record to your object and method. The problem is the parameters are getting confused as 'Types', and are therefore returning null, so when your query executes, it returns nothing because there are no records where Id='null'. Remember Apex is case insensitive, so there is no difference between Id ID and id. I hope this clarifies it.
Now, when I try to remove the public void from the test class like you suggest, a new syntax error appears. Am I missing something here?