This is following way you could write the class and scenario.
To apply locking from apex code, choose the checkbox Enable record locking and unlocking in Apex as true, upon navigating Setup->Create-> Workflows and Approvals -> Process Automation Settings
@istest
public class TestNoteOnContentDocuments {
static testmethod void NoteOnContentDoc()
{
List<Apexpages.Message> msgs;
//create testUser
System.runAs(testUser)
{
//create an account
Account acct = new Account(Name='TEST_ACCT');
insert acct;
ContentVersion contentVersion = new ContentVersion(
Title = 'Penguins',
PathOnClient = 'Penguins.jpg',
VersionData = Blob.valueOf('Test Content'),
IsMajorVersion = true
);
insert contentVersion;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
//create ContentDocumentLink record for that account
ContentDocumentLink cdl = New ContentDocumentLink(
LinkedEntityId = acct.id, ContentDocumentId = documents[0].Id, shareType = 'V');
insert cdl;
List<Account> accts = [SELECT Id from Account WHERE Name = 'TEST_ACCT'];
//lock the account record.
Approval.LockResult[] lrList = Approval.lock(accts, false);
Test.startTest();
try { delete documents; }
catch (Exception dmx)
{
Boolean expectedExceptionThrown = dmx.getMessage().contains('Approval pending. You do not have the permission to edit/delete this note/attachment, please contact your administrator.') ? true : false;
System.assertEquals(expectedExceptionThrown, true);
}
//Also it can tested like this through Apexpages.Message
List<Apexpages.Message> msgs;
msgs = ApexPages.getMessages();
boolean isErrorMessage = false;
for(ApexPages.Message msg : msgs)
{
if (msg.getSummary().contains('Approval pending.') )
{
isErrorMessage = true;
}
}
System.debug('testing messages');
System.assert(isErrorMessage);
Test.stopTest();
}
}