+ Start a Discussion
Apex developer 21Apex developer 21 

Can anyone help me to make a unit test for this it keeps giving me 37%

trigger NoteOnContentversion on ContentVersion (before insert, before update) {
	for (ContentVersion c : Trigger.new)  {
        
            for (ContentDocumentLink link : [
                SELECT LinkedEntityId 
                FROM ContentDocumentLink
                WHERE ContentDocumentId = :c.ContentDocumentId
            ]){
                Id parentId = link.LinkedEntityId;
                System.debug('link.LinkedEntityId '+ link);
                
                Boolean isOrderRegel = parentId.getSObjectType() == Orderregel__c.SObjectType;
                
                if (isOrderRegel && Approval.isLocked(parentId)){
                  c.addError('Approval pending. You do not have the permission to edit this note, Please contact your administrator.');
                }
            }
        
    }
}
 
@IsTest
private class NoteOnContentversion {
	
    @IsTest static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account a = new Account(
        	Name = '123', 
            Account_Status_DS__c = 'Strategic'
        );
        insert a;
        
        Orderregel__c orderregel = new Orderregel__c(
            Account__c = a.Id,
            Orderbegindatum__c = Date.today()
        );
        
        insert orderregel;
        Approval.lock(orderregel);
        
        ContentVersion n = new ContentVersion();
       
        insert n;
        
        ContentDocumentLink cl  = new ContentDocumentLink();
        
        insert cl;
               
        // act
        DmlException caughtException;
        try {
            insert cl;
        	insert n;
        } catch(DmlException e) {
            caughtException = e;
        }
        
        // assert
        System.assertNotEquals(null, caughtException);
    }   
}

 
Best Answer chosen by Apex developer 21
Ariel GorfinkelAriel Gorfinkel
Please try the following:
@IsTest(seeAllData=false)
private class TestNoteOnContentversion {
	
    @IsTest 
    static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account a = new Account(
        	Name = '123', 
            Account_Status_DS__c = 'Strategic'
        );
        insert a;
        
        Orderregel__c orderregel = new Orderregel__c(
            Account__c = a.Id,
            Orderbegindatum__c = Date.today()
        );
        
        insert orderregel;
        
        ContentDocumentLink cl  = new ContentDocumentLink(ContentDocumentId = a.id, LinkedEntityId = orderregel.Id + '');

        ContentVersion n = new ContentVersion(ContentDocumentId = a.id);

        Approval.lock(orderregel);               
        // act
        try {
            insert cl;
        	insert n;
            System.assert(false); //An exception should be thrown. Should not get here.
        } catch(DmlException e) {
            //Suggestion:
            //System.assert(e.getMessage().contains('Approval pending. You do not have the permission to edit this note, Please contact your administrator.'));
        }
    }   
}

 

All Answers

Guillaume Minero 8Guillaume Minero 8

Instead of inserting the newly created records at 2 places, only try doing so where you can catch the exception(s) -

try{Here}catch(DmlException e){handle exception}

and try to manage your assertion in the excepotion handling.

 

Ariel GorfinkelAriel Gorfinkel
@IsTest
private class NoteOnContentversion {
	
    @IsTest static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account a = new Account(
        	Name = '123', 
            Account_Status_DS__c = 'Strategic'
        );
        insert a;
        
        Orderregel__c orderregel = new Orderregel__c(
            Account__c = a.Id,
            Orderbegindatum__c = Date.today()
        );
        
        insert orderregel;
        
        ContentDocumentLink cl  = new ContentDocumentLink(ContentDocumentId = a.id, LinkedEntityId = orderregel.Id);

        ContentVersion n = new ContentVersion(ContentDocumentId = a.id);

        Approval.lock(orderregel);               
        // act
        DmlException caughtException;
        try {
            insert cl;
        	insert n;
            System.assert(false); //An exception should be thrown. Should not get here.
        } catch(DmlException e) {
            //Suggestion:
            //System.assert(e.getMessage().contains('Approval pending. You do not have the permission to edit this note, Please contact your administrator.');
            // I'd also suggest using a custom label instead
        }
    }   
}

Try the above
Apex developer 21Apex developer 21
Hi Ariel thanks for responding. I tried the above but this gave me 0% coverage? 
Apex developer 21Apex developer 21
this is the result with the first approach
Guillaume Minero 8Guillaume Minero 8
        try{
            insert a;
            insert n;
            insert cl;
        }
        catch(DmlException e){
            System.assert(e.getMessage().contains('ERROR MESSAGE'), e.getMessage());
        }

As Mentioned, try inserting ONLY in your TRY in order to catch the exception and use System.assert against your error message to validate.
Make sure your assertion is within range of your catch.

I hope this helps, if not, you can always post developer oriented questions here (http://salesforce.stackexchange.com/)
Ariel GorfinkelAriel Gorfinkel
0% probably means we had an exception....
Maybe there's some validation on ContentDocumentId and I cannot add a reference to the account there?
Lets try null instead?
@IsTest
private class NoteOnContentversion {
	
    @IsTest static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account a = new Account(
        	Name = '123', 
            Account_Status_DS__c = 'Strategic'
        );
        insert a;
        
        Orderregel__c orderregel = new Orderregel__c(
            Account__c = a.Id,
            Orderbegindatum__c = Date.today()
        );
        insert orderregel;
        
        ContentDocumentLink cl  = new ContentDocumentLink(LinkedEntityId = orderregel.Id);

        ContentVersion n = new ContentVersion();

        Approval.lock(orderregel);               
        // act
        DmlException caughtException;
        try {
            insert cl;
        	insert n;
            System.assert(false); //An exception should be thrown. Should not get here.
        } catch(DmlException e) {
            //Suggestion:
            System.assert(e.getMessage().contains('Approval pending. You do not have the permission to edit this note, Please contact your administrator.'); // Todo: consider using a custom label instead
        }
    }   
}

 
Ariel GorfinkelAriel Gorfinkel
Another thing to consider is using "seeAllData=false". In case you have more data that can match (same account / doc link with null ContentDocumentId) - they can mess up the test.
Apex developer 21Apex developer 21
Hi Ariel I tried the last even with "seeAllData=false" but its still giving me 0% test
Apex developer 21Apex developer 21
@Guillaume Minero 8 also tried your solution but still 0%
Ariel GorfinkelAriel Gorfinkel
As a side note, it is not recommended to have a SOQL query inside a loop (as you have done in your trigger), as it may cause exceeding governing limits for SOQL queries.
When a test fails, it covers no code. On which line did the test fail and what is the failure cause?
Perhaps no exception was thrown, which caused assertion failure?
Apex developer 21Apex developer 21
Hi Ariel, this is the fail:

07:56:54:267 EXCEPTION_THROWN [22]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NoteOnContentversion: execution of BeforeInsert

07:56:54:000 FATAL_ERROR caused by: System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.
Ariel GorfinkelAriel Gorfinkel
Please try the following:
@IsTest(seeAllData=false)
private class TestNoteOnContentversion {
	
    @IsTest 
    static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account a = new Account(
        	Name = '123', 
            Account_Status_DS__c = 'Strategic'
        );
        insert a;
        
        Orderregel__c orderregel = new Orderregel__c(
            Account__c = a.Id,
            Orderbegindatum__c = Date.today()
        );
        
        insert orderregel;
        
        ContentDocumentLink cl  = new ContentDocumentLink(ContentDocumentId = a.id, LinkedEntityId = orderregel.Id + '');

        ContentVersion n = new ContentVersion(ContentDocumentId = a.id);

        Approval.lock(orderregel);               
        // act
        try {
            insert cl;
        	insert n;
            System.assert(false); //An exception should be thrown. Should not get here.
        } catch(DmlException e) {
            //Suggestion:
            //System.assert(e.getMessage().contains('Approval pending. You do not have the permission to edit this note, Please contact your administrator.'));
        }
    }   
}

 
This was selected as the best answer