Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I've done writing code on salesforce and in order to release the unit tests have to cover at least 75%.

What I am facing is that the classOne that calls methods from classTwo also have to cover classTwo's unit test within classOne even though it is done in classTwo file already.

File MyClassTwo

public with sharing class ClassTwo {

    public String method1() {

        return 'one';

    }

    public String method2() {

        return 'two';

    }

    public static testMethod void testMethod1() {

        ClassTwo two = new ClassTwo();

        String out = two.method1();

        system.assertEquals(out, 'one'); //valid    

    }

    public static testMethod void testMethod2() {

        ClassTwo two = new ClassTwo();

        String out = two.method2();

        system.assertEquals(out, 'two'); // valid

    }

}

File MyClassOne

 public with sharing class ClassOne {
    public String callClassTwo() {
        ClassTwo foo = new ClassTwo();
        String something = foo.method1();
        return something;
    }
    public static testMethod void testCallClassTwo() {
        ClassOne one = new ClassOne();
        String out = one.callClassTwo();
        system.assertEquals(out, 'one');
    }
}

1 Answer

0 votes
by (32.1k points)

I designed an Apex class called TestHelper for all my mock objects. I use constants (static final) for values that I might need elsewhere and public static fields for objects. Works great and since no methods are used, no test coverage is needed.

public without sharing class TestHelper {

public static final string testPRODUCTNAME = 'test Product Name';

public static final string testCOMPANYID = '2508'; 

public static Account testAccount {

    get{

        Account tAccount = new Account(

            Name = 'Test Account',

            BillingStreet = '123 Main St',

            BillingCity = 'Dallas',

            BillingState = 'TX',

            BillingPostalCode = '75234',

            Website = 'http://www.google.com',

            Phone = '222 345 4567',                

            Subscription_Start_Date__c = system.today(),

            Subscription_End_Date__c = system.today().addDays(30),

            Number_Of_Seats__c = 1,

            companyId__c = testCOMPANYID,

            ZProduct_Name__c = testPRODUCTNAME);      

        insert tAccount;

        return tAccount; 

    }

}

}

Browse Categories

...