for eg private static test(color c)
2 Answers
One problem is that your method is declared as private
which means other classes can't access it. This includes Unit Tests. There's a few ways around this. You could:
- Declare the method as
public
- Call instead a
public
method that calls theprivate
one - Use the
@TestVisible
annotation
As @Mahmood mentioned in the comments:
It is accepting a workbook object. You need to first create a workbook object and then call your method and pass it on.
So, to cover this class you could do something as simple as creating a new Workbook
object and calling it (working under the assumption you've changed private
to public
or another workaround):
@isTest static void test_method_one() {
Workbook wrkbk = new Workbook(
// Assign fields values here...
);
INSERT wrkbk;
myClassThatContainsTheMethod.processActionPlan(wrkbk);
}
That would start covering code, but you're going to have to create multiple instances of Workbook
objects that match each case if your long if/then
statements.
For instance, your first if/then
statement is:
if(sheet.sheet.contains('Action Plan')) {
Thus you need to ensure that your sheet.sheet
contains the String
"Action Plan". If you don't do this, you'll find that a very small percentage of the method is covered because if as in this case it doesn't match "Action Plan" it'll skip everything inside and continue on. In this instance, to the end of the method.
I strongly advise looking over some Trailhead modules on Unit Testing. In a nutshell, think of it as programatically executing your classes and methods and asserting that the value returned does what you expect.
For instance, if you have a class like below:
public class Example {
public static Integer addNumbers(Integer x, Integer y) {
return x + y;
}
}
Your unit test might look something like this:
@isTest static void test_method_one() {
Integer a = 10, b = 17; // Create two numbers to add together
Integer result = Example.AddNumbers(a, b); // Call the class and the method and assign it to another variable
System.assertEquals(27, result); // We know 10 + 17 = 27, so that's what we expect. If `result` returns something different, we know there's a problem and the test will fail
}
-
i have tried this Workbook wrkbk = new Workbook(); insert wrkbk; CRSImport.processActionPlan(wrkbk); getting error- Invalid type: Workbook– user42459Commented Mar 1, 2017 at 12:20
-
@user42459
Workbook
isn't a standard object. If it's custom you'd need to append__c
or if it's formed as part of a wrapper class you'd need to build that in the Unit Test. Commented Mar 1, 2017 at 12:25 -
WorkBook is not wrapper class. public class WorkBook { public List<Sheet> sheets { get; set; } }– user42459Commented Mar 1, 2017 at 12:32
Private methods are not visible in Test Classes. Hence use @TestVisible annotation so that the method is accessible for running tests only. Use as shown below.
@TestVisible private static void processActionPlan(Workbook workbook) {
your code
}
Refer this link TestVisible Annotation for further details.