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
Satheesh1105Satheesh1105 

Apex Test Class For Soql select Query

I dont know what's the error in this test class
In test Class Line no 4 List acc didn't return any value
Apex class
public with sharing class ShowOppurtunity {
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> showoppurtunity(String AccId) {
        system.debug(AccId);
        List<Opportunity> lstOpportunity = [SELECT Name,StageName,TrackingNumber__c,LeadSource,Description FROM Opportunity WHERE AccountId = :AccId ];
        system.debug(lstOpportunity);
        return lstOpportunity;
    }
}

Apex test Class

@isTest
public with sharing class ShowOppurtunityTest {
    static testmethod void testData(){
        Test.startTest();
        List<Account> acc = [SELECT Id FROM ACCOUNT LIMIT 1];
        system.debug('1');
        system.debug(acc);
        if (acc != null) {
        List<Opportunity> opp =[SELECT Name,StageName,TrackingNumber__c,LeadSource,Description FROM Opportunity WHERE AccountId = :acc[0].Id];
        List<Opportunity> opt = ShowOppurtunity.showoppurtunity(String.valueOf(acc[0].Id));
        system.debug(opp);
        system.debug(opt);
        System.assertEquals(opp,opt);
        Test.stopTest();
        }
    }
}
 
Best Answer chosen by Satheesh1105
RituSharmaRituSharma
You need to create test data in your test class and only then your query will return the data. Refer this URl for details:

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_data

All Answers

RituSharmaRituSharma
You need to create test data in your test class and only then your query will return the data. Refer this URl for details:

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_data
This was selected as the best answer
Satheesh1105Satheesh1105
Thanks RituSharma
RituSharmaRituSharma
Please mark my answer as best answer if that was helpful. This would help others in future.