function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DiutGDiutG 

Need help to coverage my unit test

Hi, i want to cover my class but i don't know what i do that.

public with sharing class LCCTRL_GenBop {

    @AuraEnabled
    public static void insertFile(Id oppId, String base64FileData, Opportunity recordData) {

        try {
            Id[] contentDocIdLs = new Id[]{};
    
            ContentDocumentLink[] ctLinkLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :oppId];
            
            for(ContentDocumentLink ctLink : ctLinkLs) {
                contentDocIdLs.add(ctLink.ContentDocumentId);
            }
    
            ContentVersion[] cvLs = [SELECT Id FROM ContentVersion WHERE ContentDocumentId  IN :contentDocIdLs AND IsLatest = true AND Type_fileupload__c = 'BOP'];
    
            String fileTitle = 'BOP--'+ (recordData.Name!=null ? recordData.Name : '') + '_V'+(cvLs.size()+1);
            ContentVersion cv = new ContentVersion(Type_fileupload__c = 'BOP',  VersionData = EncodingUtil.base64Decode(base64FileData), Title = fileTitle, PathOnClient = fileTitle + '.' + 'pdf');
            insert cv;

            cv = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = : cv.Id];
    
            insert new ContentDocumentLink(LinkedEntityId = oppId, ContentDocumentId = cv.ContentDocumentId);

        } catch (AuraHandledException e) {
            throw new AuraHandledException(e.getMessage());
        }
    }

    @AuraEnabled
    public static Map<String,Object> getData(Id oppId) {
        Map<String,Object> dataMap = new Map<String,Object>();

        Opportunity opp = [SELECT Id, Name, Customer_Selection_Criteria_Price__c, Customer_Selection_Criteria_Time__c, Customer_Selection_Criteria_Quality__c, Customer_Selection_Criteria_Location__c, Customer_Selection_Criteria_Reputation__c, Customer_Selection_Criteria_Relationship__c,
            Account.Name, TOLABEL(Account.Country__c), TOLABEL(Account.Type), Account.Market_cap_Investors__c, Account.Dedicated_to_therapeutic_fields__c, Account.Clinical_or_marketed_product_pipeline__c, Account.Linked_to_other_CDMO__c, Account.Cultural_fit__c, Account.Expertise__c, Account.Capacity__c, Account.Strategic__c, Account.Relationship__c, 
            Nature_of_the_molecule__c, Z_Indication__c, TOLABEL(Stage_of_Development__c), Sales_expected_at_Peak_Sale__c, Estimated_probability_of_Success__c, When__c, Which_technology__c, First_source__c, Dead_line_to_deliver_the_offer__c, Dead_line_to_select_the_CDMO__c
                FROM Opportunity WHERE Id = :oppId LIMIT 1];

        dataMap.put('record', opp);

        return dataMap;
    }
}


I have try this to make test for enter in the method testInsertFile but he doesn't work.

In the test class is my data used in the org for test.

@isTest
public with sharing class TEST_LCCTRL_GenBop {

    @TestSetup
    static void setup(){

        Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Account.getRecordTypeInfosByName();

        Schema.RecordTypeInfo recordtype = rtMapByName.get('Commercial');

        Account account4Test = new Account(name = 'TEST ACCOUNT'
                , Country__c = 'US'
                , CurrencyIsoCode = 'EUR'
                , Type = 'Commercial'
                , RecordTypeId = recordtype.RecordTypeId);

        insert account4Test;
        
        Product2 product = TestCreate_Product2.Create_Product2();

        GMID_Product__c gmid = new GMID_Product__c();

        gmid.Z_Commercial_Product__c = product.Id;
        gmid.Z_GMID_ext_key__c = 'GMID_EXT_001';
        gmid.Z_GMID_GUOM_to_KGM__c = '0';
        gmid.Z_GMID_Status__c = true;
        gmid.Z_CRI__c = 100;
        insert gmid;

        Term_of_payment__c temOfPayment = new Term_of_payment__c();
        temOfPayment.Z_EN_Description__c = 'TEST';
        temOfPayment.Z_Source_date__c = Date.today();
        insert temOfPayment;


        Opportunity opportunity = new Opportunity();
        opportunity.AccountId = account4Test.Id;
        opportunity.Z_Product__c = product.Id;
        opportunity.Z_GMID_Product__c = gmid.Id;
        opportunity.StageName= TRGHDL_Opportunity.QUALIFICATION;
        opportunity.Name = 'TEST1';
        opportunity.Z_Service_comments__c = 'TEST1';
        opportunity.CloseDate = Date.today().addDays(30);

        opportunity.Z_Incoterms__c = 'CIP';
        opportunity.Z_Incoterms_2__c = 'TEST';
        opportunity.Z_Terms_of_Payment__c = temOfPayment.Id;
        
        insert opportunity;
    }
    
    @IsTest
    private static void testInsertFile(){

        
        String fileData = 'Title';
        String fileDateBase64 = EncodingUtil.base64Encode(Blob.valueOf(FileData));

        Opportunity opportunity = [SELECT AccountId, StageName FROM Opportunity WHERE Z_Service_comments__c ='TEST1' LIMIT 1];

        ContentVersion[] cvLs = [SELECT Id, ContentDocumentId FROM ContentVersion];
        Test.startTest();
        insert opportunity;

    }
        
    @IsTest
    private static void getData(){


    }
}
Best Answer chosen by DiutG
CharuDuttCharuDutt
Hii DiutG
Try below Test Class 90% Coverage
@isTest
public class LCCTRL_GenBopTest {
    @isTest
    public static void unitTest(){
        opportunity opp = new opportunity();
        opp.Name='test OPp';
        Opp.CloseDate = system.today();
        opp.StageName ='Closed Won';
        insert opp;
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body to be insert in test class for testing the'); 
        ContentVersion contentVersion_1 = new ContentVersion(
            Title='SampleTitle', 
            PathOnClient ='SampleTitle.jpg',
            VersionData = bodyBlob, 
            origin = 'H'
        );
        insert contentVersion_1;
        
        ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId 
                                           FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];
        
        ContentDocumentLink contentlink = new ContentDocumentLink();
        contentlink.LinkedEntityId = opp.id;
        contentlink.contentdocumentid = contentVersion_2.contentdocumentid;
        contentlink.ShareType = 'V';
        insert contentlink;
        Test.startTest();
        LCCTRL_GenBop.insertFile(opp.Id, 'base64FileData', opp);
        LCCTRL_GenBop.getData(opp.Id);
        Test.stopTest();
        
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii DiutG
Try below Test Class 90% Coverage
@isTest
public class LCCTRL_GenBopTest {
    @isTest
    public static void unitTest(){
        opportunity opp = new opportunity();
        opp.Name='test OPp';
        Opp.CloseDate = system.today();
        opp.StageName ='Closed Won';
        insert opp;
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body to be insert in test class for testing the'); 
        ContentVersion contentVersion_1 = new ContentVersion(
            Title='SampleTitle', 
            PathOnClient ='SampleTitle.jpg',
            VersionData = bodyBlob, 
            origin = 'H'
        );
        insert contentVersion_1;
        
        ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId 
                                           FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];
        
        ContentDocumentLink contentlink = new ContentDocumentLink();
        contentlink.LinkedEntityId = opp.id;
        contentlink.contentdocumentid = contentVersion_2.contentdocumentid;
        contentlink.ShareType = 'V';
        insert contentlink;
        Test.startTest();
        LCCTRL_GenBop.insertFile(opp.Id, 'base64FileData', opp);
        LCCTRL_GenBop.getData(opp.Id);
        Test.stopTest();
        
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class. It gives you 89% test coverage.

The main issue in your test class is you are not inserting contentdocument, Lilnk and version.
@Istest
public class LCCTRL_GenBoptEST {
 @IsTest
    private static void testInsertFile(){
        String fileData = 'Title';
        String fileDateBase64 = EncodingUtil.base64Encode(Blob.valueOf(FileData));

       Account acc= new Account();
    acc.Name='sample';
    insert acc;
    
    Opportunity opp= new Opportunity();
    opp.Name='sample';
    opp.AccountId=acc.id;
    opp.CloseDate=system.today();
    opp.StageName='Prospecting';
    insert opp;
         ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        content.Type_fileupload__c='BOP';
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=opp.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        
        insert contentlink;
    LCCTRL_GenBop.insertFile(opp.id,fileDateBase64,opp);
       map<String,object> mapapp= LCCTRL_GenBop.getData(OPP.ID);
        system.assertEquals(1, mapapp.size());
        
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
mukesh guptamukesh gupta
Hi,

Please use below code:-
 
@isTest
public with sharing class TEST_LCCTRL_GenBop {

    @TestSetup
    static void setup(){

        Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Account.getRecordTypeInfosByName();

        Schema.RecordTypeInfo recordtype = rtMapByName.get('Commercial');

        Account account4Test = new Account(name = 'TEST ACCOUNT'
                , Country__c = 'US'
                , CurrencyIsoCode = 'EUR'
                , Type = 'Commercial'
                , RecordTypeId = recordtype.RecordTypeId);

        insert account4Test;
        
        Product2 product = TestCreate_Product2.Create_Product2();

        GMID_Product__c gmid = new GMID_Product__c();

        gmid.Z_Commercial_Product__c = product.Id;
        gmid.Z_GMID_ext_key__c = 'GMID_EXT_001';
        gmid.Z_GMID_GUOM_to_KGM__c = '0';
        gmid.Z_GMID_Status__c = true;
        gmid.Z_CRI__c = 100;
        insert gmid;

        Term_of_payment__c temOfPayment = new Term_of_payment__c();
        temOfPayment.Z_EN_Description__c = 'TEST';
        temOfPayment.Z_Source_date__c = Date.today();
        insert temOfPayment;


        Opportunity opportunity = new Opportunity();
        opportunity.AccountId = account4Test.Id;
        opportunity.Z_Product__c = product.Id;
        opportunity.Z_GMID_Product__c = gmid.Id;
        opportunity.StageName= TRGHDL_Opportunity.QUALIFICATION;
        opportunity.Name = 'TEST1';
        opportunity.Z_Service_comments__c = 'TEST1';
        opportunity.CloseDate = Date.today().addDays(30);

        opportunity.Z_Incoterms__c = 'CIP';
        opportunity.Z_Incoterms_2__c = 'TEST';
        opportunity.Z_Terms_of_Payment__c = temOfPayment.Id;
        
        insert opportunity;
		
		ContentVersion content=new ContentVersion();
		content.Type_fileupload__c = 'BOP';
		content.IsLatest  = true;
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=opportunity.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers';
        insert contentlink;		
    }
    
    @IsTest
    private static void testInsertFile(){

        
        String fileData = 'Title';
        String fileDateBase64 = EncodingUtil.base64Encode(Blob.valueOf(FileData));

        Opportunity opp = [SELECT AccountId, StageName FROM Opportunity WHERE Z_Service_comments__c ='TEST1' LIMIT 1];

        ContentDocumentLink[] ctLinkLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :opp.Id];
		ContentVersion[] cvLs = [SELECT Id FROM ContentVersion WHERE ContentDocumentId  IN :ctLinkLs[0].ID IsLatest = true AND Type_fileupload__c = 'BOP'];
        Test.startTest();
		LCCTRL_GenBop.insertFile(opp.Id, String base64FileData, opp) 
		Test.StopTest();

    }
        
    @IsTest
    private static void getData(){


    }
}
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
DiutGDiutG

Hi everyone, i appreciate all of your answer, i have marked the best answer for me after test.

All of your answer help me to understand the process to do a class test, very thanks all !