0

I tried to test my method which is using inner class.

Wrote like this, and got error Invalid type: innerclass.

Apex

public class myclass {
public class innerclass {  
        @AuraEnabled
        public string name { get; set; } 
        @AuraEnabled
        public List<innerclass>innerclass { get; set;} 
        }  
    }
@AuraEnabled
public static List<innerclass> mymethod(String item){
        List<innerclass> result = new List<innerclass>();
        //Process
        return result;
    } 
}

Test class

@isTest static void mymethodtest(){
          List<innerclass>result = myclass.mymethod('test');
        system.debug(result);
    }

I think there is some method that I can access inner class from my test class.

I found @TestVisible, but it wasn't work.

How can I test my method?

2

Please find below corrected test class after fixing your main class:

You need to access inner class via main class.

As a general rule , it is "MainClass.InnerClass"

@isTest
public class myclassTest{
    @isTest static void mymethodtest(){
          List<myclass.innerclass>result = myclass.mymethod('test');
        system.debug(result);
    }
}

Your main class has compilation errors . Below is your fixed main class :

public class myclass {
public class innerclass {  
        @AuraEnabled
        public string name { get; set; } 
        @AuraEnabled
        public List<innerclass>innerclass { get; set;} 
}  
    //} This brace was extra
@AuraEnabled
public static List<innerclass> mymethod(String item){
        List<innerclass> result = new List<innerclass>();
        //Process
        return result;
    } 
}

Also , since your member variables are public , @TestVisible is not required.

  • Please explain how the original class was modified. It's unclear just by casual reading what the problem you've observed was. I had to copy-paste into an editor just to discover what you were trying to say. Also, it's likely just a typo, as the OP is getting a different error, so I'm not sure it should be at the top of the answer (or, possibly, even at all). I'd suggest addressing the main error they've stated first, then add the additional notes lower in the answer. – sfdcfox Nov 26 '19 at 5:48
  • MainClass.InnerClass enabled me to solve the problem. Thanks. – Hayato Nov 26 '19 at 6:00
  • Editing the answer as per guidlines from @sfdcfox – Rohit C Nov 26 '19 at 6:02
  • +1 That looks a lot better. Keep up the great work! Looking forward to your future contributions. 😊 – sfdcfox Nov 26 '19 at 6:11
  • Thank you , @sfdcfox – Rohit C Nov 26 '19 at 14:26
1

Maybe try this:

List<myclass.innerclass>result = myclass.mymethod('test');

Based on feedback from sfdxfox, Child class can only be accessed via parent class. And the first post explained pretty well in details, thanks

  • Your answer would be more useful if you explained exactly what was wrong to begin with, and why that line of code fixes the problem. Of course, you're exactly right, but having more information would likely be valuable to future readers. – sfdcfox Nov 26 '19 at 5:50

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.