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
kathyanikathyani 

Test Coverage

Hi,


I am trying to create a testMethod for test coverage of a functionality. Test method name cannot have parameters. How do we call a method which has parameters inside this test method?

Example:

static testMethod void testgenerateActivityForSuspendedBill()  {

        createCurrentDS(dsList, date.newInstance(2007, 10, 01), date.newInstance(2007, 10, 31), '(111) 555-5555') ;

}

Severity and Description    Path    Resource    Location    Creation Time    Id
Save error: Method does not exist or incorrect signature: createCurrentDS(LIST:SOBJECT:DeviceService__c, Date, Date, String)    DMPortal2_Apex_Staging/src/unpackaged/classes    ActivityService.cls    line 151    1226525337544    22864

What is the mistake I am doing?

Quick help would be appreciated.


thanks,
kathyani


tmatthiesentmatthiesen
What is createCurrentDS?  Seems like this static method does not exist or you don't have the right arguments.

Here is a simple example:

public class Red{

public static boolean isColor(){
return true;
}
}

and the supporting  test class:
@isTest
private class ColorTest{

static testmethod void testStuff(){
system.assert(red.isColor());

}
}
You can create all of your data in the testmethod and instantiate the methods within th testmethod or perform DML which invokes the corresponding trigger logic.
kathyanikathyani
It is not a static method. It does have correct number of arguments. Signature of the method looks fine but giving the syntax error, not sure what else I can change.

public  DeviceService__c createCurrentDS(List<DeviceService__c> dsList, Date billStartDate, Date billEndDate, String billServiceNumber) {


}
kathyanikathyani
static testMethod void testgenerateActivityForSuspendedBill()  {

       DeviceService__c d  = createCurrentDS(dsList, date.newInstance(2007, 10, 01), date.newInstance(2007, 10, 31), '(111) 555-5555') ;

}
JonPJonP
Let's look at a class in its entirety:

Code:
public class SampleClass {

public SampleClass() {
// empty default constructor, optional
}

public DeviceService__c createCurrentDS(List<DeviceService__c> dsList, Date billStartDate, Date billEndDate, String billServiceNumber) {
// do something
return new DeviceService__c();
}

static testMethod void testgenerateActivityForSuspendedBill() {
SampleClass c = new SampleClass();
DeviceService__c d = c.createCurrentDS(dsList, date.newInstance(2007, 10, 01), date.newInstance(2007, 10, 31), '(111) 555-5555') ;

System.assert(...); // test something
}
}

What you're missing is the red-highlighted code above, in which you instantiate your class.  Since createCurrentDS() is not a static method, it can only be called on an instance of the class.




tmatthiesentmatthiesen
take a look at my example again.  Create a test class and reference your class.method in the testmethod:

if your method is not static then in the test method add the following:

YOURCLASSNAME sample = new YOURCLASSNAME();
List<ServiceService__c> dsList = new List<ServiceService__c>();
//populate your list then call the method:
sample.createCurrentDS(dsList, date.newInstance(2007, 10, 01), date.newInstance(2007, 10, 31), '(111) 555-5555') ;

Please review the Apex docs: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_testing.htm
kathyanikathyani
Yes, thanks that worked. That was a silly mistake on my part.