Skip to main content

Hello, I need help to cover the test coverage, until now it 52 percentage. #Salesforce Developer

@isTest 

public class UpdateAccountStatusSchedulerTest {

private static testMethod void testScheduler() {

        Test.startTest();

        List<id> accountIds = new List<id>();

        Account a = TestUtils.createAccount();

        accountIds.add(a.Id);

        Opportunity opty = TestUtils.createOpportunity(a);

        Opportunity opt1 = [select Id, AccountId from Opportunity where AccountId = :opty.AccountId]; 

        accountIds.add(opt1.AccountId);

        Contract crt = TestUtils.createContract(a);

        Contract c1 = [select Id, AccountId from Contract where AccountId = :crt.AccountId];

        accountIds.add(c1.AccountId);

        Task tk1 =  new task(whatid=a.Id,Priority='Normal',Priority__c='Normal',Subject='Dial',Createddate= system.now(),Status='Completed');

        insert tk1;

        Task tk = [select Id,AccountId,whatid from task where AccountId =: tk1.whatid ];{

            if(tk.AccountId != null && !accountIds.contains(tk.AccountId)) {

            accountIds.add(tk.AccountId);

        }

        }

        

         system.assertEquals(accountIds[0], tk.AccountId);

        Case c = new Case(Subject='TestCase', Status='Open', SuppliedName='Name', SuppliedCompany='Company',SuppliedPhone='1234567', SuppliedEmail='foo@example.com', 

                          Product__c='Qt', Platform__c='Windows XP',AccountId=a.id);

        insert c;

        Case c2 = [select Id,AccountId FROM Case where AccountId =: c.AccountId ];{

            if(c2.AccountId != null && !accountIds.contains(c2.AccountId)) {

            accountIds.add(c2.AccountId);

        }

        }

        system.assertEquals(accountIds[0], c2.AccountId);

        

       UpdateAccountStatusScheduler scheduler = new UpdateAccountStatusScheduler();

        scheduler.execute(null);

        Test.stopTest();

        system.assertEquals(3, accountIds.size());

        system.assertEquals(accountIds[0], opt1.AccountId);

        system.assertEquals(accountIds[0], c1.AccountId);

        } 

}

3 risposte
  1. 7 gen 2023, 21:32

    To increase your test coverage, you can try the following:

    1. Add more test methods to your test class. Each test method should test a different code path or scenario.
    2. Make sure that your test methods are actually exercising the code you want to test. This means that you should be creating test data and calling methods or making assertions in your test methods.
    3. Make sure that your test methods are independent of each other. This means that each test method should set up its own test data and not rely on the data created by other test methods.
    4. Use the Test.loadData() method to insert test records into your org for your test methods to use. This can be more efficient than inserting records manually in your test methods.

    I hope this helps!

0/9000