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
Amit Jadhav 13Amit Jadhav 13 

i need a help on test class

public with sharing class OpportunitylistViewCom {
    @AuraEnabled
    public static List<OpportunityLineItem> getOpportunityLineItem(Id accountId,String sortField, boolean isAsc){
        String query = 'Select id,Opportunity.Name,Opportunity.Account.name,Opportunity.RecordType.Name,Opportunity.type,Opportunity.Amount,Opportunity.StageName,Opportunity.Parent_Asset__r.InstallDate,Product_Name__c,UnitPrice,TotalPrice,ListPrice,End_Date__c,Related_To__c ';
            query += 'From OpportunityLineItem where Opportunity.AccountId =:accountId';
        if (sortField != '') {
            query += ' order by ' + sortField;
            if (isAsc) {
                query += ' asc';
            } else {
                query += ' desc';
            }
        }
        list <OpportunityLineItem> oppList1;
        try {
            oppList1 = Database.query(query);
            List < OpportunityLineItem > oppList = new List < OpportunityLineItem > ();
            for (OpportunityLineItem c: oppList1) {
                oppList.add(c);
            }
            return oppList;
        } 
        catch (Exception ex) {
            // for handle Exception
            return null;
        }
    }
   
    @AuraEnabled
    public static list < Attachment > fetchopportunity(Id accountId) {
        set<ID> oppIDs = new set<ID>();
        for(Opportunity opp :[Select id from opportunity where AccountId=:accountId]){
            oppIDs.add(opp.id);
        }
        list < Attachment > returnoppList = new List <Attachment> ();
        List < Attachment > lstopp = [select id,name,LastModifiedDate,CreatedBy.name,Parent.Name, Owner.Name, BodyLength from Attachment where ParentId IN:oppIDs];
        for (Attachment c: lstopp) {
            returnoppList.add(c);
        }
        return returnoppList;
    }
}

 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Amit,
Create a test class
1. insert an account
2.insert opportunity
3.insert Opportunity lineitem and relate to to above opportunity
4.Call the getOpportunityLineItem method by providing all the parameters
5.insert an attachment with parentid as above opportunity id.

Please refer below link which might help you in this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards​​​​​​​
Foram Rana RForam Rana R
Hi Amit,

​​​​​​​I hope you are doing well .....!!

Please use the below code:
@isTest
public class OpportunitylistViewComTest {
	
    static testMethod void validateHelloWorld() {
        
        Account acc= new Account();
        acc.Name = 'Test';
        acc.First_Name__c = 'test1';
        acc.Last_Name__c = 'test1';
        insert acc;
        
        opportunity opp = new opportunity();
        opp.Name = 'test123';
        opp.AccountId = acc.Id;
        opp.CloseDate = System.today();
        opp.StageName = 'Prospecting';
        insert opp;
        
        Attachment att = new Attachment();
        att.ParentId = opp.Id;
        att.Name = 'test';
        att.Body = blob.valueOf('test');
        insert att;
        
        OpportunitylistViewCom.getOpportunityLineItem(acc.Id, 'Name', true);
        OpportunitylistViewCom.fetchopportunity(acc.Id);
    }
}
It Cover 80% you can modified it as per your requirement and make sure you put all required field while inserting the record in test class.


Hope this helps you.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana