In an apex class and test class I have a custom class that I use to compile custom attachment result records.

public class attachmentResult{ 
      public string name {get;set;}
      public string type {get;set;}
      public string id {get;set;}
      public string url {get;set;}
      public string objType {get;set;}

This list is populated by another method within this apex class that queries attachments.

In my test method I am compiling a list of this inner class:

@isTest static void myTestMethod() {

List<attachmentResult> responseAttachments = new List<attachmentResult>();

Attachment testAttachment = [select Id, Name, ContentType from Attachment where ParentId = :testCase.Id];

        attachmentResult aResult = new attachmentResult();     
        aResult.Id = testAttachment.Id;
        aResult.type = testAttachment.ContentType;
        aResult.Name = testAttachment.Name;
        aResult.objType = 'Form';

    myApexController myController= new myApexController();
    myController.responseAttachments = responseAttachments; 


In my apex class (myApexController) responseAttachments is a public list:

public list<attachmentResult> responseAttachments {get;set;}

Upon trying to save the setting of the list in the apex class with the list I'm generating in my test class, I am getting an Illegal Assignment from List to List error.

  • 1
    Please share the code where you have declared myController – Tushar Sharma May 7 at 16:07
  • 2
    How is myController.responseAttachments declared? – Jayant Das May 7 at 16:07
  • @S.B. You will need to provide the snippet from myApexController and the declaration of responseAttachments variable in that class. – Jayant Das May 7 at 16:12
  • 1
    The important bit of your description is missing - as Jayant said - you need to show how the controller is structured and the "responseAttachments" value declared. – Phil W May 7 at 16:15
  • Is AttachmentResult a custom class? Have you defined it as an inner class or is it top level? – Adrian Larson May 7 at 16:18

If you have a top level class, you can find it in your list of Apex Classes. These top level classes can have inner classes like so:

public class MyController // top level class
    public class MyInnerClass { } // inner class
    public List<MyInnerClass> attribute;

If you declare an identically named inner class in your test, it is still a different type.

class MyTest
    public class MyInnerClass { } // not the same type
    @IsTest static void brokenDemo()
        MyController controller = new MyController();
        controller.attribute = new List<MyInnerClass>();
        // will fail because of type mismatch
    @IsTest static void workingDemo()
        MyController controller = new MyController();
        controller.attribute = new List<MyController.MyInnerClass>();
        // will succeed because types match

Within your test, there are two similar but different types: MyTest.MyInnerClass and MyController.MyInnerClass. They are not compatible. If you reference just MyInnerClass within MyTest, it will be of the former type. If you reference it within MyController, it will be of the latter type.

  • Thank you, I wouldn't have thought the custom class maintained a "type", in retrospect that makes sense. The terminology clarifications were also beneficial. – S.B. May 7 at 16:56
  • It is a strongly typed language. Every variable has a type, and the declared and assigned types must always match. – Adrian Larson May 7 at 17:13

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.