0

Hi I have done many extensions of standard controllers but am new to custom controllers. I have a controller for approvals and am trying to test but am hitting many errors.

Class with wrapper

public class approval_controller {
    public class item_wrapper {
        public item_wrapper(id id,string name, string proname, string status) {
            this.id = id;
            this.name = name;
            this.status = status;
            this.proname = proname;
        }
        public id id { get; set; }
        public string name { get; set; }
        public string proname {get; set; }
        public string status {get; set; }
    }

    public list<item_wrapper> items_to_approve { get; set; }

    public approval_controller() {
        items_to_approve = new list<item_wrapper>();
        for(ProcessInstanceWorkItem item:[select processinstance.targetobjectid,processinstance.targetobject.name,
        processinstance.ProcessDefinition.name, processinstance.status 
         from processinstanceworkitem where actorid = :userinfo.getuserid()]) {
            items_to_approve.add(new item_wrapper(item.processinstance.targetobjectid,item.processinstance.targetobject.name, 
            item.processinstance.ProcessDefinition.name, item.processinstance.status));
        }
    }
}

Test start

@isTest
global class approval_controllerTest{

    static testMethod void testapproval_controller() {

           ProcessInstance testInstance = new ProcessInstance();
            testInstance.targetObjectId = new Contact(LastName = 'Test').id;
            ProcessInstanceWorkitem testP = new ProcessInstanceWorkitem(ActorId = UserInfo.getUserId(), 
                                                                        ProcessInstance = testInstance);

    approval_controller ap = new approval_controller();
    approval_controller.item_wrapper ac = new  approval_controller.item_wrapper();

    }
}

Error Error: Compile Error: Constructor not defined: [approval_controller.item_wrapper].() at line 12 column 43

0

1 Answer 1

1

The error in this line

approval_controller.item_wrapper ac = new  approval_controller.item_wrapper();

it's because there is no default constructor and you need to create a parameterized constructor and need to pass exact number of parameter as you have in your constructor.

approval_controller.item_wrapper ac = new approval_controller.item_wrapper(Id a, String b, String cb,String d);

But you really don't need this lines because when your for loop will run it will automatically cover these lines of code

for(ProcessInstanceWorkItem item:[select processinstance.targetobjectid,processinstance.targetobject.name,
        processinstance.ProcessDefinition.name, processinstance.status 
         from processinstanceworkitem where actorid = :userinfo.getuserid()]) {
            items_to_approve.add(new item_wrapper(item.processinstance.targetobjectid,item.processinstance.targetobject.name, 
            item.processinstance.ProcessDefinition.name, item.processinstance.status));
        }

Also you need to insert your test data insert testP; // I don't find this in your test class so just a suggestion.

4
  • this is not clear to me yet... also the wrapper class seems more work - i feel like i should be able to run this query more simply as an extension
    – jaw999
    Commented Oct 31, 2016 at 18:57
  • @jaw999 when the for loop run it will cover your wrapper class you don't need to do anything extra here Commented Oct 31, 2016 at 18:59
  • ok, i am not sure about defining the constructor still.
    – jaw999
    Commented Oct 31, 2016 at 19:02
  • @jaw999 you have parameterized constructor so you need to pass parameters when you define the wrapper class constructor in test class or you need to create a default constructor with out parameters in your main class Commented Oct 31, 2016 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .