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
chandrashekar Jangitichandrashekar Jangiti 

Hi Friends..Am struggling to write Test class for below code for Getting Lead

Hi friends...
Am struggling to write Test class for below code for Getting Lead...please help me t's urgent..



public static Lead getLead(String recordId) {
  Lead lead = [Select id,Status,Status__c  from Lead where id=:recordId]; 
  return lead;
    } 



Thanks
Chandu
Best Answer chosen by chandrashekar Jangiti
PawanKumarPawanKumar
suppose the class is as follows:

public class BusinessLogic{
public static Lead getLead(String recordId) {
  Lead lead = [Select id,Status  from Lead where id=:recordId]; 
  return lead;
    }
    }

Test Class:
---------------------


@isTest 
public class BusinessLogic_Test{

    @isTest
    static void testGetlead() {
        Lead testLead = new Lead();
        testLead.Lastname = 'test';
        testLead.Company='testCompany';
        insert testLead;
        
        Lead createdLead = BusinessLogic.getLead(testLead.Id);
        System.assertNotEquals(createdLead,null);
   }
}

Please mark it best if it helps you. Thanks.

Regards,
Pawan Kumar

All Answers

sfdcMonkey.comsfdcMonkey.com
try something like this in your test class :
 
Lead olead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry');

insert olead;

className.getLead(olead.Id);

Thanks let us know if it helps you 
PawanKumarPawanKumar
suppose the class is as follows:

public class BusinessLogic{
public static Lead getLead(String recordId) {
  Lead lead = [Select id,Status  from Lead where id=:recordId]; 
  return lead;
    }
    }

Test Class:
---------------------


@isTest 
public class BusinessLogic_Test{

    @isTest
    static void testGetlead() {
        Lead testLead = new Lead();
        testLead.Lastname = 'test';
        testLead.Company='testCompany';
        insert testLead;
        
        Lead createdLead = BusinessLogic.getLead(testLead.Id);
        System.assertNotEquals(createdLead,null);
   }
}

Please mark it best if it helps you. Thanks.

Regards,
Pawan Kumar
This was selected as the best answer
chandrashekar Jangitichandrashekar Jangiti
Thank you @Piyush and @Pawan...

Thanks
Chandu
 
Sai ThejaSai Theja
can someone help me to cover code 100%. I'm getting 66% for my test class.
Apex class:
global class ValidateBusinessHours implements Process.Plugin { 

global Process.PluginResult invoke(Process.PluginRequest request) { 
        
        DateTime dt = (DateTime) request.inputParameters.get('dt');
        
        BusinessHours bh = [SELECT Id,name FROM BusinessHours WHERE Name='Zelle Flow' limit 1];
        System.debug('Business Hours:'+bh);
        System.debug('Current Date Time'+dt);
        System.debug('Current Date time is in Business hours::'+BusinessHours.isWithin(bh.id, dt));
        Map<String,boolean> result = new Map<String,boolean>(); 
        result.put('value',BusinessHours.isWithin(bh.id, dt));
        return new Process.PluginResult(result);
    } 

    // Returns the describe information for the interface
    global Process.PluginDescribeResult describe() { 
        Process.PluginDescribeResult result = new Process.PluginDescribeResult(); 
        result.Name = 'businessHoursplugin';
        result.Tag = 'BusinessHours';
        result.inputParameters = new 
           List<Process.PluginDescribeResult.InputParameter>{ 
               new Process.PluginDescribeResult.InputParameter('dt', 
               Process.PluginDescribeResult.ParameterType.DateTime, true)
            };
         result.outputParameters = new 
           List<Process.PluginDescribeResult.OutputParameter>{
            new Process.PluginDescribeResult.OutputParameter(
                'value', 
                Process.PluginDescribeResult.ParameterType.boolean)
           };     
        return result; 
    }
}




Test Class:

@isTest
private class ValidateBusinessHours_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    BusinessHours businesshours_Obj = new BusinessHours(Name = 'Name220', IsActive = true, IsDefault = false, TimeZoneSidKey = 'Pacific/Kiritimati');
    test.stopTest();
  }
  static testMethod void test_PluginRequest_UseCase1(){
    List<BusinessHours> businesshours_Obj  =  [SELECT Id,Name from BusinessHours];
    System.assertEquals(true,businesshours_Obj.size()>0);
    ValidateBusinessHours obj01 = new ValidateBusinessHours();
    //obj01.PluginRequest(new Process.PluginRequest());
    
  }
  static testMethod void test_describe_UseCase1(){
    List<BusinessHours> businesshours_Obj  =  [SELECT Id,Name from BusinessHours];
    System.assertEquals(true,businesshours_Obj.size()>0);
    ValidateBusinessHours obj01 = new ValidateBusinessHours();
    obj01.describe();
  }

    static testMethod void test_describe_UseCase2(){
    List<BusinessHours> businesshours_Obj  =  [SELECT Id,Name from BusinessHours];
    System.assertEquals(true,businesshours_Obj.size()>0);
    ValidateBusinessHours obj01 = new ValidateBusinessHours();
    //businesshours_Obj[0].id='';
    businesshours_Obj[0].Name='hgvhv';
    obj01.describe();
  }
}