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
Cynthia Rodgers 12Cynthia Rodgers 12 

Test Class is only at 33%

I have created an apex class and a test class but my code coverage is at 33% and I need it to be at 75% or higher. I am not sure how to increase the test class i created using test generator, can you tell me how to increase my code coverage?

My Apex class is evaluating the account fields TSS Designation = Temporary and TSS Temporary End Date.  When the designation is temporary and the end date is today, then this class will update those fields to null.  I have this class setup on the scheduler to run daily.

apex class: update tss fields
public class update_tss_fields implements Schedulable, Database.Batchable<sObject>
{
    public void execute( SchedulableContext context )
    {
        Database.executeBatch( this );
    }
    public Database.QueryLocator start( Database.BatchableContext context )
    {
        return Database.getQueryLocator
        (   'SELECT Id, TSS_Designation__c, TSS_Temporary_End_Date__c '
        +   'FROM Account '
        +   'WHERE TSS_Temporary_End_Date__c = TODAY'
        );
    }

    public void execute( Database.BatchableContext context, List<Account> accounts )
    {
        for ( Account account : accounts )
        {
            account.TSS_Designation__c = null;
            account.TSS_Temporary_End_Date__c = null;
        }
        update accounts;
    }
    public void finish( Database.BatchableContext context )
    {
        // nothing to do here...
    }
}



 
Best Answer chosen by Cynthia Rodgers 12
Steven NsubugaSteven Nsubuga
Try this
@isTest 
private class update_tss_fields_Test{ 
    @testSetup static void setupTestData(){
        test.startTest(); 
        Account account_Obj = new Account(Name = 'Name705',TSS_Designation__c = 'Temporary', 
       TSS_Temporary_End_Date__c = Date.today());
        Upsert account_Obj; 
        test.stopTest();
    }
    
    static testMethod void testBatch(){
        update_tss_fields obj01 = new update_tss_fields();
        Database.executeBatch(obj01, 200);
    }
    
    static testmethod void testScheduler(){
        String CRON_EXP = '0 0 0 15 3 ? *';
        Test.startTest();

            String jobId = System.schedule('update_tss_fields',  CRON_EXP, new update_tss_fields());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();
    }
}

 

All Answers

Cynthia Rodgers 12Cynthia Rodgers 12
the test class i created using test generator (app downloaded from app exchange) produced the following test class

@isTest private class update_tss_fields_Test{ @testSetup static void setupTestData(){ test.startTest(); Account account_Obj = new Account(Name = 'Name705', Household__c = false, Top_Prospect__c = false, Banker_Originated__c = false, Converted_Lead__c = false, Profile_Complete__c = false, Treasury_Customer__c = false, LLC_BI__Automated_Financials__c = false, LLC_BI__Employee_Relationship__c = false, LLC_BI__Financials_Opt_Out__c = false, LLC_BI__Reg_O_Relationship__c = false, LLC_BI__Backup_Withholding_Exempt_Indicator__c = false, LLC_BI__Backup_Withholding_N_A_Indicator__c = false, LLC_BI__Form_W8_Model_2_Indicator__c = false, LLC_BI__Form_W8_Owner_Trust_Indicator__c = false, LLC_BI__Form_W8_Signer_Indicator__c = false, LLC_BI__Form_W8_Treaty_Limitation_Provision_Ind__c = false, LLC_BI__Form_W8_Treaty_Qualified_Resident_Status__c = false, LLC_BI__Form_W9_Seperat_TIN_Certification_Ind__c = false, LLC_BI__Previous_Financial_Account_Indicator__c = false, nSBA__Bankruptcy__c = false, nSBA__Business_At_Least_12_Months_Old__c = false, nSBA__Convicted_Criminal__c = false, nSBA__Current_Lawsuit__c = false, nSBA__Exporter__c = false, nSBA__Federal_Employee__c = false, nSBA__Form_4506T_Account_Transcript_Req__c = false, nSBA__Form_4506T_Form_W2_1099_Series_Trans_Req__c = false, nSBA__Form_4506T_Has_Authority_To_Sign__c = false, nSBA__Form_4506T_Record_Of_Account_Req__c = false, nSBA__Form_4506T_Return_Transcript_Req__c = false, nSBA__Form_4506T_Verification_Of_Nonfiling__c = false, nSBA__Possible_Affiliations_Indicator__c = false, nSBA__Prior_SBA_Loan__c = false, Franchise__c = false, Wealth_Client__c = false, CL_Best_Efforts__c = false, CL_Bulk_Mandatory__c = false, CL_FHA__c = false, CL_FNLMC__c = false, CL_FNMA__c = false, CL_GNMA__c = false, CL_Jumbo__c = false, CL_Single_Mandatory__c = false, CL_TPO__c = false, CL_USDA__c = false, CL_VA__c = false, CL_Warehouse_Client__c = false, BDO_Qualified_Checkbox__c = false, Woman_Owned_Business__c = false, CL_FHLMC__c = false, Active_Seller__c = false, nBRIDGER__OFAC_CSR_Relationship__c = false, nBRIDGER__OFAC_Reg_W_Relationship__c = false, nSBA__Disability_Insurance__c = false, Debt_Funds__c = false, COR__c = false, Charge_Account_Analysis__c = false, Dual_Admins__c = false, Dummy_Relationship_Name__c = false, Exception_Pricing__c = false, Mortgage_Finance__c = false, Non_Accrual__c = false, Perform_Annual_Review__c = false, Permanent_Waiver__c = false, SCRA__c = false, Signature_Services_Client__c = false, Single_Admin__c = false, TCB_Employee__c = false, TSS_Designation__c = 'Temporary', TSS_Temporary_End_Date__c = Date.today(), Temporary_Waiver__c = false, iFACs__c = false, Current_Customer__c = false, Product_Gap__c = false, LLCLP__Primary_Borrower__c = false, nSBA__Applicant_Is_Presently_Suspended__c = false, nSBA__Business_Has_Outstanding_Debt__c = false, nSBA__Is_Government_Guaranteed_Loan_Defaulted__c = false, nSBA__Is_Government_Guaranteed_Loan_Delinquent__c = false, nSBA__No_Former_SBA_Employee_Is_Owner_Partner__c = false, nSBA__No_GS13_Government_Employee_Owns_10_Pct__c = false, nSBA__No_Member_of_Congress_Owns_10_Percent__c = false, nSBA__No_SBAC_Or_SCORE_Member_Owns_10_Percent__c = false, nSBA__No_SBA_Employee_Owns_10_Percent_Or_More__c = false, nSBA__Prior_Government_Loan__c = false, nSBA__Revenues_From_Gambling_Or_Sexual_Nature__c = false, Top_Client__c = false); Upsert account_Obj; test.stopTest();
}
static testMethod void testBatch(){
update_tss_fields obj01 = new update_tss_fields();
Database.executeBatch(obj01, 200);
}
}
Waqar Hussain SFWaqar Hussain SF
Hi Cynthia,

See updated test class, this should work for you. 
 
@isTest private class update_tss_fields_Test {
    @testSetup static void setupTestData() {
        test.startTest();
        Account account_Obj = new Account(Name = 'Name705', Household__c = false, Top_Prospect__c = false, Banker_Originated__c = false, Converted_Lead__c = false, Profile_Complete__c = false, Treasury_Customer__c = false, LLC_BI__Automated_Financials__c = false, LLC_BI__Employee_Relationship__c = false, LLC_BI__Financials_Opt_Out__c = false, LLC_BI__Reg_O_Relationship__c = false, LLC_BI__Backup_Withholding_Exempt_Indicator__c = false, LLC_BI__Backup_Withholding_N_A_Indicator__c = false, LLC_BI__Form_W8_Model_2_Indicator__c = false, LLC_BI__Form_W8_Owner_Trust_Indicator__c = false, LLC_BI__Form_W8_Signer_Indicator__c = false, LLC_BI__Form_W8_Treaty_Limitation_Provision_Ind__c = false, LLC_BI__Form_W8_Treaty_Qualified_Resident_Status__c = false, LLC_BI__Form_W9_Seperat_TIN_Certification_Ind__c = false, LLC_BI__Previous_Financial_Account_Indicator__c = false, nSBA__Bankruptcy__c = false, nSBA__Business_At_Least_12_Months_Old__c = false, nSBA__Convicted_Criminal__c = false, nSBA__Current_Lawsuit__c = false, nSBA__Exporter__c = false, nSBA__Federal_Employee__c = false, nSBA__Form_4506T_Account_Transcript_Req__c = false, nSBA__Form_4506T_Form_W2_1099_Series_Trans_Req__c = false, nSBA__Form_4506T_Has_Authority_To_Sign__c = false, nSBA__Form_4506T_Record_Of_Account_Req__c = false, nSBA__Form_4506T_Return_Transcript_Req__c = false, nSBA__Form_4506T_Verification_Of_Nonfiling__c = false, nSBA__Possible_Affiliations_Indicator__c = false, nSBA__Prior_SBA_Loan__c = false, Franchise__c = false, Wealth_Client__c = false, CL_Best_Efforts__c = false, CL_Bulk_Mandatory__c = false, CL_FHA__c = false, CL_FNLMC__c = false, CL_FNMA__c = false, CL_GNMA__c = false, CL_Jumbo__c = false, CL_Single_Mandatory__c = false, CL_TPO__c = false, CL_USDA__c = false, CL_VA__c = false, CL_Warehouse_Client__c = false, BDO_Qualified_Checkbox__c = false, Woman_Owned_Business__c = false, CL_FHLMC__c = false, Active_Seller__c = false, nBRIDGER__OFAC_CSR_Relationship__c = false, nBRIDGER__OFAC_Reg_W_Relationship__c = false, nSBA__Disability_Insurance__c = false, Debt_Funds__c = false, COR__c = false, Charge_Account_Analysis__c = false, Dual_Admins__c = false, Dummy_Relationship_Name__c = false, Exception_Pricing__c = false, Mortgage_Finance__c = false, Non_Accrual__c = false, Perform_Annual_Review__c = false, Permanent_Waiver__c = false, SCRA__c = false, Signature_Services_Client__c = false, Single_Admin__c = false, TCB_Employee__c = false, TSS_Designation__c = 'Temporary', TSS_Temporary_End_Date__c = Date.today(), Temporary_Waiver__c = false, iFACs__c = false, Current_Customer__c = false, Product_Gap__c = false, LLCLP__Primary_Borrower__c = false, nSBA__Applicant_Is_Presently_Suspended__c = false, nSBA__Business_Has_Outstanding_Debt__c = false, nSBA__Is_Government_Guaranteed_Loan_Defaulted__c = false, nSBA__Is_Government_Guaranteed_Loan_Delinquent__c = false, nSBA__No_Former_SBA_Employee_Is_Owner_Partner__c = false, nSBA__No_GS13_Government_Employee_Owns_10_Pct__c = false, nSBA__No_Member_of_Congress_Owns_10_Percent__c = false, nSBA__No_SBAC_Or_SCORE_Member_Owns_10_Percent__c = false, nSBA__No_SBA_Employee_Owns_10_Percent_Or_More__c = false, nSBA__Prior_Government_Loan__c = false, nSBA__Revenues_From_Gambling_Or_Sexual_Nature__c = false, Top_Client__c = false);
        Upsert account_Obj;
		
		update_tss_fields obj01 = new update_tss_fields();
        Database.executeBatch(obj01, 200);
		
        test.stopTest();
    }
}

Let me know If you have any concern.

Thanks​
Cynthia Rodgers 12Cynthia Rodgers 12
Hi Wagar,
Thanks for replying back.  I just tested it and now i am getting no code coverage and it says 0/0 test methods passed.  
Waqar Hussain SFWaqar Hussain SF
Are you seeing any error message?

If not then try below one. 
@isTest private class update_tss_fields_Test {
    @testSetup static void setupTestData() {
        
        Account account_Obj = new Account(Name = 'Name705', Household__c = false, Top_Prospect__c = false, Banker_Originated__c = false, Converted_Lead__c = false, Profile_Complete__c = false, Treasury_Customer__c = false, LLC_BI__Automated_Financials__c = false, LLC_BI__Employee_Relationship__c = false, LLC_BI__Financials_Opt_Out__c = false, LLC_BI__Reg_O_Relationship__c = false, LLC_BI__Backup_Withholding_Exempt_Indicator__c = false, LLC_BI__Backup_Withholding_N_A_Indicator__c = false, LLC_BI__Form_W8_Model_2_Indicator__c = false, LLC_BI__Form_W8_Owner_Trust_Indicator__c = false, LLC_BI__Form_W8_Signer_Indicator__c = false, LLC_BI__Form_W8_Treaty_Limitation_Provision_Ind__c = false, LLC_BI__Form_W8_Treaty_Qualified_Resident_Status__c = false, LLC_BI__Form_W9_Seperat_TIN_Certification_Ind__c = false, LLC_BI__Previous_Financial_Account_Indicator__c = false, nSBA__Bankruptcy__c = false, nSBA__Business_At_Least_12_Months_Old__c = false, nSBA__Convicted_Criminal__c = false, nSBA__Current_Lawsuit__c = false, nSBA__Exporter__c = false, nSBA__Federal_Employee__c = false, nSBA__Form_4506T_Account_Transcript_Req__c = false, nSBA__Form_4506T_Form_W2_1099_Series_Trans_Req__c = false, nSBA__Form_4506T_Has_Authority_To_Sign__c = false, nSBA__Form_4506T_Record_Of_Account_Req__c = false, nSBA__Form_4506T_Return_Transcript_Req__c = false, nSBA__Form_4506T_Verification_Of_Nonfiling__c = false, nSBA__Possible_Affiliations_Indicator__c = false, nSBA__Prior_SBA_Loan__c = false, Franchise__c = false, Wealth_Client__c = false, CL_Best_Efforts__c = false, CL_Bulk_Mandatory__c = false, CL_FHA__c = false, CL_FNLMC__c = false, CL_FNMA__c = false, CL_GNMA__c = false, CL_Jumbo__c = false, CL_Single_Mandatory__c = false, CL_TPO__c = false, CL_USDA__c = false, CL_VA__c = false, CL_Warehouse_Client__c = false, BDO_Qualified_Checkbox__c = false, Woman_Owned_Business__c = false, CL_FHLMC__c = false, Active_Seller__c = false, nBRIDGER__OFAC_CSR_Relationship__c = false, nBRIDGER__OFAC_Reg_W_Relationship__c = false, nSBA__Disability_Insurance__c = false, Debt_Funds__c = false, COR__c = false, Charge_Account_Analysis__c = false, Dual_Admins__c = false, Dummy_Relationship_Name__c = false, Exception_Pricing__c = false, Mortgage_Finance__c = false, Non_Accrual__c = false, Perform_Annual_Review__c = false, Permanent_Waiver__c = false, SCRA__c = false, Signature_Services_Client__c = false, Single_Admin__c = false, TCB_Employee__c = false, TSS_Designation__c = 'Temporary', TSS_Temporary_End_Date__c = Date.today(), Temporary_Waiver__c = false, iFACs__c = false, Current_Customer__c = false, Product_Gap__c = false, LLCLP__Primary_Borrower__c = false, nSBA__Applicant_Is_Presently_Suspended__c = false, nSBA__Business_Has_Outstanding_Debt__c = false, nSBA__Is_Government_Guaranteed_Loan_Defaulted__c = false, nSBA__Is_Government_Guaranteed_Loan_Delinquent__c = false, nSBA__No_Former_SBA_Employee_Is_Owner_Partner__c = false, nSBA__No_GS13_Government_Employee_Owns_10_Pct__c = false, nSBA__No_Member_of_Congress_Owns_10_Percent__c = false, nSBA__No_SBAC_Or_SCORE_Member_Owns_10_Percent__c = false, nSBA__No_SBA_Employee_Owns_10_Percent_Or_More__c = false, nSBA__Prior_Government_Loan__c = false, nSBA__Revenues_From_Gambling_Or_Sexual_Nature__c = false, Top_Client__c = false);
        Upsert account_Obj;

	test.startTest();
	
		update_tss_fields obj01 = new update_tss_fields();
        Database.executeBatch(obj01);
		
        test.stopTest();
    }
}

 
Cynthia Rodgers 12Cynthia Rodgers 12
Ok let me test this one but there are no errors.
Waqar Hussain SFWaqar Hussain SF
Sorry my bad.
The above class will not run. Pleas run the below class.
@isTest 
private class update_tss_fields_Test {
    static testMethod void testBatch(){
	
       Account account_Obj = new Account(Name = 'Name705', Household__c = false, Top_Prospect__c = false, Banker_Originated__c = false, Converted_Lead__c = false, Profile_Complete__c = false, Treasury_Customer__c = false, LLC_BI__Automated_Financials__c = false, LLC_BI__Employee_Relationship__c = false, LLC_BI__Financials_Opt_Out__c = false, LLC_BI__Reg_O_Relationship__c = false, LLC_BI__Backup_Withholding_Exempt_Indicator__c = false, LLC_BI__Backup_Withholding_N_A_Indicator__c = false, LLC_BI__Form_W8_Model_2_Indicator__c = false, LLC_BI__Form_W8_Owner_Trust_Indicator__c = false, LLC_BI__Form_W8_Signer_Indicator__c = false, LLC_BI__Form_W8_Treaty_Limitation_Provision_Ind__c = false, LLC_BI__Form_W8_Treaty_Qualified_Resident_Status__c = false, LLC_BI__Form_W9_Seperat_TIN_Certification_Ind__c = false, LLC_BI__Previous_Financial_Account_Indicator__c = false, nSBA__Bankruptcy__c = false, nSBA__Business_At_Least_12_Months_Old__c = false, nSBA__Convicted_Criminal__c = false, nSBA__Current_Lawsuit__c = false, nSBA__Exporter__c = false, nSBA__Federal_Employee__c = false, nSBA__Form_4506T_Account_Transcript_Req__c = false, nSBA__Form_4506T_Form_W2_1099_Series_Trans_Req__c = false, nSBA__Form_4506T_Has_Authority_To_Sign__c = false, nSBA__Form_4506T_Record_Of_Account_Req__c = false, nSBA__Form_4506T_Return_Transcript_Req__c = false, nSBA__Form_4506T_Verification_Of_Nonfiling__c = false, nSBA__Possible_Affiliations_Indicator__c = false, nSBA__Prior_SBA_Loan__c = false, Franchise__c = false, Wealth_Client__c = false, CL_Best_Efforts__c = false, CL_Bulk_Mandatory__c = false, CL_FHA__c = false, CL_FNLMC__c = false, CL_FNMA__c = false, CL_GNMA__c = false, CL_Jumbo__c = false, CL_Single_Mandatory__c = false, CL_TPO__c = false, CL_USDA__c = false, CL_VA__c = false, CL_Warehouse_Client__c = false, BDO_Qualified_Checkbox__c = false, Woman_Owned_Business__c = false, CL_FHLMC__c = false, Active_Seller__c = false, nBRIDGER__OFAC_CSR_Relationship__c = false, nBRIDGER__OFAC_Reg_W_Relationship__c = false, nSBA__Disability_Insurance__c = false, Debt_Funds__c = false, COR__c = false, Charge_Account_Analysis__c = false, Dual_Admins__c = false, Dummy_Relationship_Name__c = false, Exception_Pricing__c = false, Mortgage_Finance__c = false, Non_Accrual__c = false, Perform_Annual_Review__c = false, Permanent_Waiver__c = false, SCRA__c = false, Signature_Services_Client__c = false, Single_Admin__c = false, TCB_Employee__c = false, TSS_Designation__c = 'Temporary', TSS_Temporary_End_Date__c = Date.today(), Temporary_Waiver__c = false, iFACs__c = false, Current_Customer__c = false, Product_Gap__c = false, LLCLP__Primary_Borrower__c = false, nSBA__Applicant_Is_Presently_Suspended__c = false, nSBA__Business_Has_Outstanding_Debt__c = false, nSBA__Is_Government_Guaranteed_Loan_Defaulted__c = false, nSBA__Is_Government_Guaranteed_Loan_Delinquent__c = false, nSBA__No_Former_SBA_Employee_Is_Owner_Partner__c = false, nSBA__No_GS13_Government_Employee_Owns_10_Pct__c = false, nSBA__No_Member_of_Congress_Owns_10_Percent__c = false, nSBA__No_SBAC_Or_SCORE_Member_Owns_10_Percent__c = false, nSBA__No_SBA_Employee_Owns_10_Percent_Or_More__c = false, nSBA__Prior_Government_Loan__c = false, nSBA__Revenues_From_Gambling_Or_Sexual_Nature__c = false, Top_Client__c = false);
        Upsert account_Obj;
		
		test.startTest();
			update_tss_fields obj01 = new update_tss_fields();
			Database.executeBatch(obj01, 200);		
        test.stopTest();
    }
}

 
Steven NsubugaSteven Nsubuga
Try this
@isTest 
private class update_tss_fields_Test{ 
    @testSetup static void setupTestData(){
        test.startTest(); 
        Account account_Obj = new Account(Name = 'Name705',TSS_Designation__c = 'Temporary', 
       TSS_Temporary_End_Date__c = Date.today());
        Upsert account_Obj; 
        test.stopTest();
    }
    
    static testMethod void testBatch(){
        update_tss_fields obj01 = new update_tss_fields();
        Database.executeBatch(obj01, 200);
    }
    
    static testmethod void testScheduler(){
        String CRON_EXP = '0 0 0 15 3 ? *';
        Test.startTest();

            String jobId = System.schedule('update_tss_fields',  CRON_EXP, new update_tss_fields());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();
    }
}

 
This was selected as the best answer
Cynthia Rodgers 12Cynthia Rodgers 12
Apex Test Execution Screen
Cynthia Rodgers 12Cynthia Rodgers 12
Steven your test class ran and the testing results were  2/2 test methods passed.   My code coverage is now at 50% (6/12).  How do i improve on your code to get it to 75% or better?
Steven NsubugaSteven Nsubuga
Cynthia, could you check your the logs to see if any errors were generated, particularly during the insertion of the account in the test. Some trigger or automation may be affecting the creation of the account.
I tested my code before posting it and I had a 100% test coverage.
100% Test Coverage
Cynthia Rodgers 12Cynthia Rodgers 12
Steven, I checked and I'm not fininding any errors in the log.  There are account validation rules but nothing that woud affect upserting the account record or that would cause an error.  The test exectution results show pass and the duration is 0.00 for both the test batch and test scheduler.
Cynthia Rodgers 12Cynthia Rodgers 12
Hey Steven, I turned off my process builder that makes the update and the code coverage is now at a 100%.  Thank you again.