0

I am facing an issue but I don't know what I'm doing wrong. I'm writing a test class for a code which has this 2 lines:

public static Blob getPkpass(){
    // more code here...
    List<ContentVersion> archivoLogo = [SELECT id, ContentDocument.Title, VersionData FROM ContentVersion WHERE ContentDocument.Title = 'logo@2x'];
    List<ContentVersion> archivoIcon = [SELECT id, ContentDocument.Title, VersionData FROM ContentVersion WHERE ContentDocument.Title = 'icon@2x'];
    // more code here...
}

The code is working fine but in the test class when this method is executed, I get this error in the list:

List index out of bounds: 0.

The query works perfectly outside the test class. I have also tried with @TestVisible but I get the same result

What I'm doing wrong???

0

The thing you got is that test class does not see any data in your org. Add this files in @TestSetup or add @isTest(SeeAllData=true) annotation to your test method. Also note that you cannot use both TestSetup and SeeAllData at the same time.

2

The issue arises because you haven't inserted test data. I recommend you insert it in @TestSetup method of your TestClass. In the very basic case you can do it next way:

@IsTest
public with sharing class TestClass {

    @TestSetup
    static void dataSetup() {
        ContentVersion contentVersion = new ContentVersion();
        contentVersion.PathOnClient = 'test.txt';
        contentVersion.Title = 'Test file';
        contentVersion.VersionData = Blob.valueOf('Test Data');

        insert contentVersion;

        ContentDocument document = [
                SELECT Id
                FROM ContentDocument
        ][0];
        document.Title = 'logo@2x';
        update document;
    }

    // Test methods

}

After that, these entities will exist in DB from the test method of your TestClass. I would really recommend you to use @IsTest(SeeAllData=true) only in extremely exceptional cases.

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.