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
Kate WalshKate Walsh 

I have written a working Apex class for an email template (It works) now I need to write an Apex test to move an Apex class to production

I do not need a trigger for it to work in the Sandbox...all I am doing is using SQL to pull the Child Line itmes into an Email Template driven by the Parent RMA Object.

Below is my Apex Class and below that is my VF email template.  It works fine in my sandbox, but the Apex class has 0%code coverage so I cannot movie into Production....I will really appreciate any help

Apex Class (Parts Orders (po) is the master/parent record and PROL are the Detail/Child records (line items) that need to show up in the Parts Order driven email template):

public  class poTemplt
{
    public Id poId {get;set;}
    public List<Parts_Reqs_Orders_Line__c> getpols()
    {
        List<Parts_Reqs_Orders_Line__c> pol;
        pol = [SELECT Id,Name,PROL_Expected_Quantity__c,PROL_Date_Required_By__c,
PROL_Line_Status__c,PROL_Parts_Reqs_Orders__c,PROL_Product_Model__c,PROL_Product_Model__r.name,PROL_Model_Description__c,PROL_Serial_Number__r.SVMXC__Serial_Lot_Number__c,PROL_Serial_Number__c,
PROL_Tracking_Number__c,Related_Case__c,Related_Case__r.CaseNumber 
FROM Parts_Reqs_Orders_Line__c WHERE PROL_Parts_Reqs_Orders__c =: poId];
        return pol;
    }
}

Visualforce Component (VF Component is a list on the Email template with all the line items associated with the RMA's ID:
<apex:component controller="poTemplt" access="global"> <apex:attribute name="PartOdersId" type="Id" description="Id of the Part Order" assignTo="{!poId}"/> <table border = "2" cellspacing = "2"> <tr> <td>RMA Line Number</td> <td>Line Status</td> <td>Related Case</td> <td>Model</td> <td>Model Description</td> <td>Quantity</td> <td>Serial Number</td> <td>Date Required By</td> <td>Tracking Number</td> </tr> <apex:repeat value="{!pols}" var="p"> <tr> <td>{!p.Name}</td> <td>{!p.PROL_Line_Status__c}</td> <td>{!p.Related_Case__r.CaseNumber}</td> <td>{!p.PROL_Product_Model__r.name}</td> <td>{!p.PROL_Model_Description__c}</td> <td>{!p.PROL_Expected_Quantity__c}</td> <td>{!p.PROL_Serial_Number__r.SVMXC__Serial_Lot_Number__c}</td> <td>{!p.PROL_Date_Required_By__c}</td> <td>{!p.PROL_Tracking_Number__c}</td> </tr> </apex:repeat> </table> </apex:component>

I have read through the documentation on Apex testing and most is based on the trigger, but I haven't need a trigger for this fundtionality and I cannot set the Apex Class test up based on the little I have learned from the docuementation.  Please help my users really need this functionality.

Thanks
Kate
 
Best Answer chosen by Kate Walsh
Hemant_SoniHemant_Soni
Hi Kate,
Try below code.
@isTest
public class poTempltTest{

static testmethod void UnitTest(){

poTemplt oPT = new poTemplt();
Id poId = oPT.poId;
oPT.getpols();
}
}
If is solved your problem then please mark it solved.
And if not then let me know.
Thanks
Hemant
 

All Answers

Hemant_SoniHemant_Soni
Hi Kate,
Try below code.
@isTest
public class poTempltTest{

static testmethod void UnitTest(){

poTemplt oPT = new poTemplt();
Id poId = oPT.poId;
oPT.getpols();
}
}
If is solved your problem then please mark it solved.
And if not then let me know.
Thanks
Hemant
 
This was selected as the best answer
Davy HuijgensDavy Huijgens
Hi Kate,

Please learn yourself to write proper test straight of the bat. 

Each test should consist of a couple of stages:
1. set up data needed for the test.
2. execute code you are testing
3. assert the results 

Testing should not be about getting coverage but actually testing your code. So if we expand Hemant_Soni example:
 
@isTest
public class poTempltTest{

  static testmethod void UnitTest(){
    // Create a Part Order with Part Orderlines
    Parts_Order__c newPO = new Part_order__c(); //Assuming Parts_order__c is your object
    insert newPO;    

    Parts_Reqs_Orders_Line__c line1 = new Parts_Reqs_Orders_Line__c();
    line1.PROL_Parts_Reqs_Orders__c = newPO.Id;
    insert line1;

    poTemplt oPT = new poTemplt();
    oPT.poId = newPO.Id;

    List<Parts_Reqs_Orders_Line__c> result;

    Test.startTest();
    result = oPT.getpols();
    Test.stopTest();

    System.assertEquals(1, result.size());
  }

}


 
Kate WalshKate Walsh
Thanks to both Hemant and Davy...I was able to fully test my Apex Class and gained the required coverage.  However, I have run into a new issue and that is with some untest Triggers and Apex Classes buried within one of our Installed Packages managed code.  Looks like it they were written back when we deployed ServiceMax.  My Inbound Change Set from the Sandbox is failing 16 tests and thereby not deploying.

Errors are below....I am currently looking at the Apex Test Classes wirtten for the non-ServiceMax tests, but if you have any additional ideas, please send them my way.


Code Coverage Failure
Your organization's code coverage is 12%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
SVMX_WorkDetail_Trgr
SVMX_InstalledProduct_DMGI_Add
SVMX_WO_TechnicalBulltin
SVMX_InstalledProduct_Add
SVMX_Technical_Bulletin_IB
Apex Test Failures 
Class NameMethod NameError MessageAddressAutocompleteControllerTestaddressAutocompleteControllerTestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.AddressAutocompleteControllerTest.addressAutocompleteControllerTest: line 27, column 1
Create_Active_Inveter_UTmyUnitTestSystem.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 01tU0000000lZ3u: [] 
Stack Trace: Class.Create_Active_Inveter_UT.myUnitTest: line 28, column 1
CreateWOTests_UTtestCreateWOfromInstalledProductSystem.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301U00000000frO. Flow error messages: <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: [] 
Stack Trace: Class.CreateWOTests_UT.testCreateWOfromInstalledProduct: line 31, column 1
PS_ServiceReport_UTSVMX_PS_ServiceReportEST_TestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.PS_ServiceReport_UT.setupTest: line 13, column 1 Class.PS_ServiceReport_UT.SVMX_PS_ServiceReportEST_Test: line 69, column 1
PS_ServiceReport_UTSVMX_PS_ServiceReport_TestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.PS_ServiceReport_UT.setupTest: line 13, column 1 Class.PS_ServiceReport_UT.SVMX_PS_ServiceReport_Test: line 41, column 1
ServiceMaxMassDataEdittest1System.QueryException: No such column 'BillingAddress' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. 
Stack Trace: Class.ServiceMaxMassDataEdit.getAccounts: line 19, column 1 Class.ServiceMaxMassDataEdit.test1: line 269, column 1
SVMX_VF_Open_Work_Orders_UTSVMX_VF_Open_Work_Orders_TestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.SVMX_VF_Open_Work_Orders_UT.SVMX_VF_Open_Work_Orders_Test: line 15, column 1
SVMX_VF_Parts_Order_Int_UTtestSystem.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead.: [] 
Stack Trace: Class.SVMX_VF_Parts_Order_Int_UT.test: line 25, column 1
SVMX_VF_PartsOrder_UTtestSystem.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead.: [] 
Stack Trace: Class.SVMX_VF_PartsOrder_UT.test: line 25, column 1
SVMX_VF_PartsOrder_UTtest2System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead.: [] 
Stack Trace: Class.SVMX_VF_PartsOrder_UT.test2: line 95, column 1
SVMX_VF_RMA_Int_UTtestSystem.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead.: [] 
Stack Trace: Class.SVMX_VF_RMA_Int_UT.test: line 26, column 1
SVMX_VF_RMA_UTtestSystem.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead.: [] 
Stack Trace: Class.SVMX_VF_RMA_UT.test: line 22, column 1
SVMX_VF_RMA_UTtest2System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead.: [] 
Stack Trace: Class.SVMX_VF_RMA_UT.test2: line 90, column 1
SVMX_VF_ServiceQuoteFromEstimates_UTSVMX_VF_ServiceQuoteFromEstimates_TestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.SVMX_VF_ServiceQuoteFromEstimates_UT.SVMX_VF_ServiceQuoteFromEstimates_Test: line 13, column 1
SVMX_Work_OrderNLines_UTSVMX_Work_OrderNLines_TestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.SVMX_Work_OrderNLines_UT.SVMX_Work_OrderNLines_Test: line 13, column 1
SVMX_Work_Order_UTSVMX_Work_Order_TestSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Complete Billing Address must be entered in order to save Account Record: [BillingStreet] 
Stack Trace: Class.SVMX_Work_Order_UT.SVMX_Work_Order_Test: line 13, column 1
Davy HuijgensDavy Huijgens
Do you insert an account in one of your tests? Becasue there are inserts not passing validations.

Please consider changing your best answers or not selecting a best answer at all. People should not come here and copy past test just to get coverage.

This is not the purpose of testing and should not be promoted as the correct solution.