You need to sign in to do that
Don't have an account?
KMK91
How to create Test data for ContentVersion in Lightning
Hi ,
Please help on this how to create test for ContentVersion i have to run this in User context. When I'm running i'm getting List out of bound exception.
Thanks,
KMK
Please help on this how to create test for ContentVersion i have to run this in User context. When I'm running i'm getting List out of bound exception.
Thanks,
KMK
Below Sample code can fulfill your requirements. Hope this will work for you.
@isTest
public class <Controller_Name_here>Controller_AC_Test {
@isTest static void <method_name_here>_Test()
{
ContentVersion contentVersionInsert = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = Blob.valueOf('Test Content Data'),
IsMajorVersion = true
);
insert contentVersionInsert;
// Test INSERT
ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 1);
// Test DELETE
<Controller_Name_here>Controller_AC.deleteContentDocument(contentVersionSelect.ContentDocumentId);
documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 0);
}
}
Please mark this as best answer if this solves your problem.
Thank you,
Ajay Dubedi
All Answers
Below Sample code can fulfill your requirements. Hope this will work for you.
@isTest
public class <Controller_Name_here>Controller_AC_Test {
@isTest static void <method_name_here>_Test()
{
ContentVersion contentVersionInsert = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = Blob.valueOf('Test Content Data'),
IsMajorVersion = true
);
insert contentVersionInsert;
// Test INSERT
ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 1);
// Test DELETE
<Controller_Name_here>Controller_AC.deleteContentDocument(contentVersionSelect.ContentDocumentId);
documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 0);
}
}
Please mark this as best answer if this solves your problem.
Thank you,
Ajay Dubedi