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
SL TanSL Tan 

Apex Test Method for Public List - Need Help

Hi

I wonder if anyone can help me to advise on the test method  I should be using to test the following apex class. I am quite new to writing the test methods into the apex classes I have created and is at a loss at to how to solve for a public list query as below;

Any pointers will be highly appreciated.

Thanks and Best Regards

SL

 

public List<Contact> getMyContacts() {
return [SELECT Id, Name, Account.Name FROM Contact
];
}
Best Answer chosen by Admin (Salesforce Developers) 
SL TanSL Tan

Dear Ron

Sorry for my delayed acknowledgement. Thank you for your guide and I am pleased to inform you that I have successfully done the Test Method achieving 100% coverage in the process, by understanding and modifying the first sample you gave. Your help is highly appreciated.

Thanks and Best Regards

SL 

All Answers

Ron HessRon Hess

this is a simple test method, only verifies that a list is produced

 

 

public class try2 { static testMethod void t1() { account a = new Account( name='foo') ; insert a; contact c = new Contact( lastname='bar', accountid=a.id); insert c; try2 myclass = new try2(); List<Contact> l = myclass.getMyContacts(); system.assert( l.size() > 0 ); } public List<Contact> getMyContacts() { return [SELECT Id, Name, Account.Name FROM Contact limit 10 ]; } }

 

 

 

SL TanSL Tan

Dear Ross

Thanks for your kind feedback. I tried to incorporate the test method you have provided into my apex class as follows and the apex class is saved nicely.

 

public class GreigeFirstDelMthPListExt { public GreigeFirstDelMthPListExt(ApexPages.StandardController controller) { } public List<Greige_Packing_First_Del_Mth__c> getGreigePackingFirstDelMths () { return [SELECT id,Name,B_RL_1_1__c,Total_Qty__c,No_of_Bales_Rolls__c,Total_Gross_Wt_Kg__c,B_RL_1_2__c,B_RL_1_3__c, B_RL_1_4__c,B_RL_1_5__c,B_RL_1_6__c,B_RL_1_7__c,B_RL_1_8__c,B_RL_1_9__c,B_RL_1_10__c,Piece_No_1_1__c,Piece_No_1_2__c, Piece_No_1_3__c,Piece_No_1_4__c,Piece_No_1_5__c,Piece_No_1_6__c,Piece_No_1_7__c,Piece_No_1_8__c,Piece_No_1_9__c,Piece_No_1_10__c, G_Wt_1_1__c,G_Wt_1_2__c,G_Wt_1_3__c,G_Wt_1_4__c,G_Wt_1_5__c,G_Wt_1_6__c,G_Wt_1_7__c,G_Wt_1_8__c,G_Wt_1_9__c,G_Wt_1_10__c, Bale_Roll_Piece_Nos__c FROM Greige_Packing_First_Del_Mth__c]; } public static testMethod void testGreigeFirstDelMthPListExt() { ApexPages.StandardController con = new ApexPages.StandardController(new Greige_First_Del_Mth_Shipment__c()); Test.startTest(); GreigeFirstDelMthPListExt ext = new GreigeFirstDelMthPListExt(con); Greige_First_Del_Mth_Shipment__c a = new Greige_First_Del_Mth_Shipment__c(Qty_Unit__c='Metres',Company_Name__c='Nexus Textile Ltd',Greige_Order_No__c='(G-040800000)') ; insert a; Greige_Packing_First_Del_Mth__c c = new Greige_Packing_First_Del_Mth__c(Qty_Unit__c='Metres',Company_Name__c=a.id,Greige_1st_Del_Mth_Shipment_No__c=a.id,Greige_Order_No__c=a.id); insert c; GreigeFirstDelMthPListExt myclass = new GreigeFirstDelMthPListExt(con); List<Greige_Packing_First_Del_Mth__c> l = myclass.getGreigePackingFirstDelMths(); system.assert( l.size() > 0 ); Test.stopTest(); } }

 However, when I run the "Run Test, there is 1 Test Failure, and it gives me the following message -"System.StringException: Invalid id: (G-040800000)" and Stack Trace -"Class.cmiplus.GreigeFirstDelMthPListExt.testGreigeFirstDelMthPListExt: line 22, column 155" information. Also, the code coverage is 33% which is the same as before I incorporate the coding sample from you.

I hope from the above apex class details you would be able to help me solve this problem as I hope by solving this part, it will take my code coverage to more than 75%. Sorry for the inconvenience caused and thanks for your patience and assistance in advance

Best Regards

SL 


 

Ron HessRon Hess

You may have to read the docs on testing to understand how to construct test methods, my sample is a poor substutite for the knowledge you gain from the docs

 

this line 

Greige_Order_No__c='(G-040800000)' 

 

is causing the error that you see.   

 

If that field is an auto number, then you must not supply it in your constructor as it will be generated by the system. 

If that field is a lookup, then you must first construct test data for the looked up object, using another insert statement to build that data.  Then pass the ID of that object into this constructor. 

 

the concept is that all data you use in your test must be constructed in your test or it will fail when installed in a different org, you cannot rely on data that exists in the org.

 

the test coverage will go up when the errors are corrected.

 

all of this is covered in the docs.

SL TanSL Tan

Dear Ron

Many thanks for our valuable feedback which I appreciate very much. Having gone through the documents again, I have changed the test method slightly as below, and it saved nicely.

public class GreigeSecondDelMthPListExt { public GreigeSecondDelMthPListExt(ApexPages.StandardController controller) { } public List<Greige_Packing_Second_Del_Mth__c> getGreigePackingSecondDelMths () { return [SELECT id,Name,B_RL_1_1__c,Total_Qty__c,No_of_Bales_Rolls__c,Total_Gross_Wt_Kg__c,B_RL_1_2__c,B_RL_1_3__c, B_RL_1_4__c,B_RL_1_5__c,B_RL_1_6__c,B_RL_1_7__c,B_RL_1_8__c,B_RL_1_9__c,B_RL_1_10__c,Piece_No_1_1__c,Piece_No_1_2__c, Piece_No_1_3__c,Piece_No_1_4__c,Piece_No_1_5__c,Piece_No_1_6__c,Piece_No_1_7__c,Piece_No_1_8__c,Piece_No_1_9__c,Piece_No_1_10__c, G_Wt_1_1__c,G_Wt_1_2__c,G_Wt_1_3__c,G_Wt_1_4__c,G_Wt_1_5__c,G_Wt_1_6__c,G_Wt_1_7__c,G_Wt_1_8__c,G_Wt_1_9__c,G_Wt_1_10__c, Bale_Roll_Piece_Nos__c FROM Greige_Packing_Second_Del_Mth__c]; } public static testMethod void testGreigeSecondDelMthPListExt() { ApexPages.StandardController con = new ApexPages.StandardController(new Greige_Packing_Second_Del_Mth__c()); List<Greige_Packing_Second_Del_Mth__c> GreigePackingSecondDelMths = new List<Greige_Packing_Second_Del_Mth__c>{}; Greige_Packing_Second_Del_Mth__c a = new Greige_Packing_Second_Del_Mth__c(); GreigePackingSecondDelMths.add(a); Test.startTest(); GreigeSecondDelMthPListExt ext = new GreigeSecondDelMthPListExt(con); insert GreigePackingSecondDelMths; test.stopTest(); List<Greige_Packing_Second_Del_Mth__c> insertedGreigePackingSecondDelMths = [SELECT Name,B_RL_1_1__c,Total_Qty__c,No_of_Bales_Rolls__c,Total_Gross_Wt_Kg__c,B_RL_1_2__c,B_RL_1_3__c, B_RL_1_4__c,B_RL_1_5__c,B_RL_1_6__c,B_RL_1_7__c,B_RL_1_8__c,B_RL_1_9__c,B_RL_1_10__c,Piece_No_1_1__c,Piece_No_1_2__c, Piece_No_1_3__c,Piece_No_1_4__c,Piece_No_1_5__c,Piece_No_1_6__c,Piece_No_1_7__c,Piece_No_1_8__c,Piece_No_1_9__c,Piece_No_1_10__c, G_Wt_1_1__c,G_Wt_1_2__c,G_Wt_1_3__c,G_Wt_1_4__c,G_Wt_1_5__c,G_Wt_1_6__c,G_Wt_1_7__c,G_Wt_1_8__c,G_Wt_1_9__c,G_Wt_1_10__c, Bale_Roll_Piece_Nos__c FROM Greige_Packing_Second_Del_Mth__c]; } }

 

However, I got following message after performing "Run Test" -"System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [cmiplus__Greige_2nd_Del_Mth_Shipment_No__c, cmiplus__No_of_Bales_Rolls__c]: [cmiplus__Greige_2nd_Del_Mth_Shipment_No__c, cmiplus__No_of_Bales_Rolls__c]" and Stack Trace-"Class.cmiplus.GreigeSecondDelMthPListExt.testGreigeSecondDelMthPListExt: line 25, column 5'.

 

Line 25, column 5 is where I have this insert line as below. I am at a loss how to rectify this. would it be of any help if i add in the debug Log Message below? Sorry to cause you all the inconvenience but hope  you would be able to help me on this. I appreciate very much. Thanks for your patience and understanding too. Best Regards - SL

 Debug Log Message:

*** Beginning Test 1: cmiplus.GreigeSecondDelMthPListExt.public static testMethod void testGreigeSecondDelMthPListExt()

20090501042439.704:Class.cmiplus.GreigeSecondDelMthPListExt.testGreigeSecondDelMthPListExt: line 24, column 38: returning from end of method public GreigeSecondDelMthPListExt<Constructor>(ApexPages.StandardController) in 0 ms
20090501042439.704:Class.cmiplus.GreigeSecondDelMthPListExt.testGreigeSecondDelMthPListExt: line 25, column 5: Insert: LIST:SOBJECT:cmiplus__Greige_Packing_Second_Del_Mth__c
20090501042439.704:Class.cmiplus.GreigeSecondDelMthPListExt.testGreigeSecondDelMthPListExt: line 25, column 5: Changing testing limit context based on DML operation of size: 1
20090501042439.704:Class.cmiplus.GreigeSecondDelMthPListExt.testGreigeSecondDelMthPListExt: line 25, column 5: DML Operation executed in 2 ms
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [cmiplus__Greige_2nd_Del_Mth_Shipment_No__c, cmiplus__No_of_Bales_Rolls__c]: [cmiplus__Greige_2nd_Del_Mth_Shipment_No__c, cmiplus__No_of_Bales_Rolls__c]

Class.cmiplus.GreigeSecondDelMthPListExt.testGreigeSecondDelMthPListExt: line 25, column 5

insert GreigePackingSecondDelMths;

 

Ron HessRon Hess

you will have to add data for these fields

 

REQUIRED_FIELD_MISSING, Required fields are missing:

[cmiplus__Greige_2nd_Del_Mth_Shipment_No__c, cmiplus__No_of_Bales_Rolls__c]:

 

 

when building a test, you are still required to specify data for all required fields, even if you have to construct additional objects beforehand  ( in the test method) so that you have data to place into these fields.

SL TanSL Tan

Dear Ron

Sorry for my delayed acknowledgement. Thank you for your guide and I am pleased to inform you that I have successfully done the Test Method achieving 100% coverage in the process, by understanding and modifying the first sample you gave. Your help is highly appreciated.

Thanks and Best Regards

SL 

This was selected as the best answer
hello_im_herehello_im_here

this test example was super helpful. thanks!