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
Semira@gmail.comSemira@gmail.com 

Help with Test code - passed with 0% coverage.

Hi team, 

This is sort of urgent request. I wrote this class which is generating a pdf for an custom object. I wrote my test class however it's not covering anything. I'm not sure what exactly I should be testing for since I still hasn't grasps the idea of the purpose of testing class other than DML statement or returned methods. Below is my apex class and test class. It is passing with (0/0) methods "Selected job is not yet complete".

/******************************************************************************
* Extension for
*/
public with sharing class ExpensePDFController {
   
    /** List of Invoice__c fields that do not exist on the VF page that need to
     *  be referenced elsewhere in the code */
    public static List<String> ADDL_FIELDS = new List<String> {
        'Name',
        'Amount__c',
        'Approved_Date__c',
        'Approved_by__c',
        'Count_Line_Items__c',
        'Date__c',
        'Date_Submitted__c',
        'Status__c',
        'Total_Amount__c'
     };
   
    /** The invoice to generate a PDF for */
    public Expense__c expense {get; set;}
   
    public List<Expense_Line_Item__c> lineItems {get; set;}
   
    public Integer getLineItemsSize() {
        return lineItems.size();
    }
   
    /** Data structure of page parameters */
  //  public Parameters params {get; set;}
   
   
    /**************************************************************************
     *
     */
    public ExpensePDFController(ApexPages.StandardController controller) {
       
        // Add any additional fields
        if (! Test.isRunningTest()) {
            controller.addFields(ExpensePDFController.ADDL_FIELDS);
        }
       
        // Initialize local variables
        Id expenseId = ApexPages.currentPage().getParameters().get('id');
        String fields = ADCUtil_Base.strJoin(',', ADDL_FIELDS, 'Id');
        //expense = (Expense__c) Database.query('SELECT Id, Name FROM Expense__c WHERE Id = :expenseId');
        expense = (Expense__c) Database.query('SELECT '+fields+' FROM Expense__c WHERE (Id = :expenseId) ORDER BY LastModifiedDate DESC LIMIT 1');
        //params  = new Parameters();
       
        // Retrieve all of the relevant line items
        lineItems = [SELECT Id, Name, Number_of_item__c, Amount__c, Code__c, Payee__c, Contact__c, Job_Number__c, Office__c, Date_of_Expense__c FROM Expense_Line_Item__c WHERE Expense__c = :expense.Id ];
       
        System.debug('ExpensePDFController: Generating Expense for '+expense);
       
    }

}

Test Class:

@isTest
public class  ExpensePDFController_Test{
public static List<String> ADDL_FIELDS = new List<String> {
        'Name',
        'Amount__c',
        'Approved_Date__c',
        'Approved_by__c',
        'Count_Line_Items__c',
        'Date__c',
        'Date_Submitted__c',
        'Status__c',
        'Total_Amount__c'
     };
   
    private static void test_init() {
   
       
        String fields = ADCUtil_Base.strJoin(',', ADDL_FIELDS, 'a01Q0000004Jrk8IAC');
        Expense__c r = (Expense__c) Database.query('SELECT '+fields+' FROM Expense__c ORDER BY LastModifiedDate DESC LIMIT 1');
       
        PageReference ref = Page.ExpensePDF;
        Test.setCurrentPage(ref);
        Apexpages.StandardController std = new Apexpages.StandardController(r);
       
        Test.startTest();
        ExpensePDFController controller = new ExpensePDFController(std);
       
        ref = Page.ExpensePDF;
        //ref.getParameters().put('dlh', '1');
        Test.setCurrentPage(ref);
        controller = new ExpensePDFController(std);
       
        Test.stopTest();
       
    }

}
Vinita_SFDCVinita_SFDC
Hi,

You have not created test data in your test class. Also you have not queried and did not make any assertion tests.

1) create/insert test data
2) Query on the the inserted data like: SELECT Id, Name, Number_of_item__c, Amount__c, Code__c, Payee__c, Contact__c, Job_Number__c, Office__c, Date_of_Expense__c FROM Expense_Line_Item__c WHERE Expense__c = "Id of the inserted expense"
3) Test the inserted Name, Number of item etc value with actual value you received on query with assertion methods.

Refer following link for sample test code:

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm

Semira@gmail.comSemira@gmail.com

I have already tried inserting data and checking aginst it. I have also tried Asserting controller itself. It is still saying the same error. 

I believe I'm supposed to check the controller and whether it is returning the correct query. However, my test class isn't working. I'm not sure how to check my construction method. It's saying there is no method to be tested or no method is being tested. It doesn't give me any error, just says "Selected job is not yet completed". I don't know why it is not completed and I don't get any code coverage no matter what I do. 

Luca PagnottaLuca Pagnotta
I think because there is not test method declared...

@isTest(SeeAllData=true)

public class TESTAccountAttachmentsCtrl{

    public static testmethod void TESTAccountAttachmentsCtrl(){

 //write test code here
    }
}