2

I've some troubles with my test class:

Here is my test class:

@isTest
public class AccountHierarchyCtrl_Test {

static testMethod void Costruttore_Test()
{


test.startTest();
.....Creation of test data.....
    test.stopTest();  

   Test.startTest();
   PageReference page1 = Page.MyPage;
   Test.setCurrentPage(page1);
   ApexPages.currentPage().getParameters().put('accId', a.Id);
   ApexPages.StandardController controller = new ApexPages.StandardController(a);
   MYPageCtrl stdController = new MyPageCtrl(controller);

   stdcontroller.AddChild(a.id, aa, 1);
   Test.stopTest();

}
}

Here is the method I want to test

   public Boolean addChild(Id parentId, Account child, integer margin) {
    if(acc.Id == parentId) {
     children.add(new MyAccount(child, margin));

        return true;
    } else if(children != null && !children.isEmpty()) {
        Boolean result = false;
        Integer i = 0;
        do {

            MyAccount myacc = children[i];  
            result = myacc.addChild(parentId,child,margin);
            i++;

        } while(!result && i < children.size());
    } 
    return false;
}

When I lauch the test I get this error:

Method does not exist or incorrect signature: [AccountHierarchyCtrl].AddChild(Id, Account, Integer)

Anyone could help me?? Thanks in advance. A

  • 1
    Is aa account record in test class? – Reshma Feb 2 '17 at 11:59
  • Yes, It's an Account. – Alessia Annella Feb 2 '17 at 12:00
  • 1
    Can you post controller code too? – Reshma Feb 2 '17 at 12:05
  • What class contains the addChild method? – Eric Feb 2 '17 at 17:37
0

You've defined method as Public boolean addChild(Id parentId, Account child, integer margin) and you're calling method as stdcontroller.AddChild(a.id, aa, 1); Check both names. Method defined has small addChild while calling statement has AddChild. Uppercase and lowercase issue. That's why you're getting an error.

  • 4
    Apex is case-insensitive. AddChild and addChild is same for Apex – Santanu Halder Feb 2 '17 at 12:27
0

From what I can see, you have a method called addChild in the controller MyPageCtrl or in the controller MyAccount. I'm not sure if you have the same method on both. But it looks like you are trying to call the method from MyAccount from the controller instance. Obviously it will throw an error because the controller doesn't have that method. Instead, an object that is inside the controller does.

Instead of calling a controller method, you should call the object MyAccount method. If the MyAccount object is a single instance within the controller, you may call it using stdController.theMyAccountObject.addChild. If it is in a list (List<MyAccount>) then you will have to use the list instance with an index (like stdController.theList[0].addChild).

It would benefit everyone who reads your question if you provided more information regarding your controller structure and what you are trying to do. Also, posting the whole controller code wouldn't be a bad idea (that is, if it doesn't contain 500+ lines...).

0

It looks like you are calling both test.startTest() and test.stopTest() methods twice in your static testMethod void Costruttore_Test(){...} method. Per the Documentation you are only allowed to call those methods once per testMethod.

Hers's a design pattern for Unit Tests that I use religiously... mostly to defend myself from declarative changes like new required fields and validation rules. You might find it useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.