unit-testingsalesforceapexsalesforce-lightning

Sales force - Test Apex Class List


I made an Apex Class in the Sandbox to call a List.
Now I need to implement it in the production. To do so, I need to implement a Test with at least 75% Success.

The Apex Class produces a List of dfind_Research_Projekt__c from which dfind_Potenzieller_Kandidat__c is the actual record, I use this list to make an iteration and show all the dfind_Research_Projekt__c on the page from dfind_Potenzieller_Kandidat__c.

This is my Apex Class:

public with sharing class dfind_Pot_Job_Application_List {
    @AuraEnabled
    
    //Get Pot Job Application List
    public static  List<dfind_Research_Projekt__c> getJobApp(Id recordId) {
        
        List<dfind_Research_Projekt__c> JobAppList =  [Select Id, Name, dfind_Potenzieller_Kandidat__c, dfind_Job__c, 
                                                       LastModifiedById, dfind_Bewerbungsdatum__c, dfind_Job_Name__c,
                                                       OwnerId
                                                       
                                                       from dfind_Research_Projekt__c 
                                                       where dfind_Potenzieller_Kandidat__c = :recordId
                                                       ORDER BY dfind_Bewerbungsdatum__c DESC NULLS LAST];
        return JobAppList;
    }
    
    
    //Get User
    @AuraEnabled
    
    public static user fetchUser(){
        User u = [select id,Name from User where id =: userInfo.getUserId()];
        return u;
    }
    
    
    
}

This is my test:

@isTest
public class TESTdfind_pot_job_app  {
    static testMethod void myUnitTest()  {
        
        //Create Data for Customer Objet
        cxsrec__Potential_candidate__c objKandi = new cxsrec__Potential_candidate__c();
        objKandi.Name = 'Test Kandidat';
        
        insert objKandi;
        
        //Create List
        List<dfind_Research_Projekt__c> listOfPotApp = new List<dfind_Research_Projekt__c>{
            new dfind_Research_Projekt__c(Name='Test Appplication'
                                          , dfind_Job__c='a0w0X000008KKB5QAO'
                                          , dfind_Potenzieller_Kandidat__c = objKandi.Id
                                          , dfind_Bewerbungsdatum__c = Datetime.now()
                                          , OwnerId= '0050X000007vz5MQAQ'),
                new dfind_Research_Projekt__c(Name='Test Appplication 1'
                                              , dfind_Job__c='a0w1x0000013aSRAAY'
                                              , dfind_Potenzieller_Kandidat__c = objKandi.Id
                                              , dfind_Bewerbungsdatum__c = Datetime.now()
                                              , OwnerId= '0050X000007vz5MQAQ'),
                new dfind_Research_Projekt__c(Name='Test Appplication 2'
                                              , dfind_Job__c='a0w1x000000JJSBAA4'
                                              , dfind_Potenzieller_Kandidat__c = objKandi.Id
                                              , dfind_Bewerbungsdatum__c = Datetime.now()
                                              , OwnerId= '0050X000007vz5MQAQ')
                };
                    
        insert(listOfPotApp);
        
        Test.startTest();  
        // Starts the scope of test
        // Now check if it is giving desired results using system.assert
        // Statement.New invoice should be created

       List<dfind_Research_Projekt__c> JobAppList = new List<dfind_Research_Projekt__c>(listOfPotApp);
 
        

        Test.stopTest();     // Ends the scope of test
        
        
         for(Integer i=0;i<JobAppList.Size();i++) {
            system.assertEquals(JobAppList[i].dfind_Potenzieller_Kandidat__c,objKandi.Id);
            System.debug(i + 'Kandidat: ' + JobAppList[i].dfind_Potenzieller_Kandidat__c + ';');
            System.debug(i + ': ' + objKandi.Id + ';');
          
        }
        
        system.assertEquals(1,1);
        
    }
}

Solution

  • The hard-coded Ids in your unit test won't work. Your unit tests executes in an isolated data context and must generate all of its own test data.

    As written, this test doesn't really do anything. It does not invoke the code you intend to test, as it must to evaluate its behavior and obtain code coverage. You'd need to call fetchUser() and getJobApp() at some point and write assertions about their return values. The assertions that are currently present are all tautological; they're guaranteed to pass and provide no information.

    See How do I write an Apex unit test? on Salesforce Stack Exchange for introductory resources.