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
mukesh gupta 8mukesh gupta 8 

testMethod

Hi Experts,

I am new in salesforce  and little bit confusse for testMethod for this class, How i can use testMethod for below class so that i can get 75% code coverage, and get the  current records form the "getTodaysMileageRecords()" function that's returning the list.

@isTest

public class MileageExtension {

private final Mileage__c mileageObj;

public MileageExtension(ApexPages.StandardController controller) {

this.mileageObj = (Mileage__c)controller.getRecord();

}

public Mileage__c[] getTodaysMileageRecords() {

String createdbyId = UserInfo.getUserId();

Mileage__c[] mileageList =  [SELECT name, miles__c
FROM Mileage__c
WHERE Date__c = TODAY
AND createdbyid = :createdbyId];
return mileageList;

}

}

Waiting for possitive reply

Many Thanks

Mukesh gupta
Marek Kosar_Marek Kosar_
Hi,

your class, without @isTest annotation:
public class MileageExtension {
  private final Mileage__c mileageObj;

  public MileageExtension(ApexPages.StandardController controller) {
    this.mileageObj = (Mileage__c)controller.getRecord();
  }

  public Mileage__c[] getTodaysMileageRecords() {
    String createdbyId = UserInfo.getUserId();
    Mileage__c[] mileageList =  [SELECT name, miles__c
      FROM Mileage__c
      WHERE Date__c = TODAY
      AND createdbyid = :createdbyId];
 
    return mileageList;
  }
}
test method should be (it's and example, not complete test method :) ):
@isTest
public class extensionTestClass {

    public static testMethod void extensionTestMethod() {
       
        Mileage__c mage = new Milage__c();
        //here you create your data, fields you need (name, miles, date...etc)
        insert mage;

        PageReference pageRef = Page.nameOfYourVFPage;
        Test.setCurrentPage(pageRef);
      
        MileageExtension controller = new MileageExtension();
        Mileage__c[] mileageTest  = controller.getTodaysMileageRecords();
        
        System.assert(/* here you put assertion to make sure your method is working correctly*/);
    }

}


 
mukesh gupta 8mukesh gupta 8
Hi Mark,

I have implement the my class according your method but got error Compilation error: Constructor not defined:  [MileageExtension].<Constructor>()

@isTest

public class ExtensionTestMethod {
  public static testMethod void ExtensionTestMethod() {
     PageReference pageRef = Page.MyMileagePage;
     Test.setCurrentPage(pageRef);

///below line show the error for Compilation error: Constructor not defined:  [MileageExtension].<Constructor>()///////////////
     MileageExtension controller = new MileageExtension();

      Mileage__c[] mileageTest  = controller.getTodaysMileageRecords();

      System.assert(/* here you put assertion to make sure your method is working correctly*/);

    }

}

Many Thanks
Mukesh
Marek Kosar_Marek Kosar_
@isTest
public class extensionTestClass {
    public static testMethod void extensionTestMethod() {
       
        Mileage__c mage = new Milage__c();
        //here you create your data, fields you need (name, miles, date...etc)
        insert mage;

        PageReference pageRef = Page.nameOfYourVFPage;
        Test.setCurrentPage(pageRef);

        ApexPages.StandardController sc = new ApexPages.standardController(mage);   
        MileageExtension controller = new MileageExtension(sc);
        Mileage__c[] mileageTest  = controller.getTodaysMileageRecords();
        
        System.assert(/* here you put assertion to make sure your method is working correctly*/);
    }
}
some corrections ^^