How does unit test work in salesforce?

I wrote code on salesforce and to produce unit tests you need to cover at least 75% .

I came across that classOnewhich calls methods from classTwoshould also cover the classTwounit test inside classOne, even if it is already done in the classTwofile.

MyClassTwo File

 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
    }

}

MyClassOne File

 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');
    }
}

The test result of MyClassOne would not return 100% test coverage, since it says that I did not include the MyClassTwo method2 () part inside the MyClassOne file.

But I already wrote a unit test for MyClassTwo inside the MyClassTwo file, as you can see.

So does this mean that I need to copy and paste the unit test into the MyClassTwo file in MyClassOne?

100% - , . ClassA ClassB....? ?

, mock- salesforce? , ..

http://sites.force.com/answers/ideaView?c=09a30000000D9xt&id=087300000007m3fAAA&returnUrl=/apex/ideaList%3Fc%3D09a30000000D9xt%26category%3DApex%2B%2526%2BVisualforce%26p%3D19%26sort%3Dpopular

UDPATE

, ClassOne 100%, classTwo method2()

+3
3

Java Salesforce;) , , ... Salesforce ( , ).

...

, ( , public String hello() )... , , MyClassA → Force.com → - , - ...

: http://dl.dropbox.com/u/709568/stackoverflow/masato_code_coverage.png

, ... , ? , , ( , , MyClassATest). - " "? , : , , , ?

+3

"" , B A, B.

( , , "" ).

. 100% - . :

  • ?

  • ?

, 2 :

  • B b, , B , , ..

  • A, B

?

: - , : B, : "+++".

.

java, mockito: http://mockito.org/

easymock: http://easymock.org/

, . , .

Java mockito:

public class aUTest {

    protected A a;

    @Mock protected B b;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        a = new A();
        ReflectionTestUtils.setField(a, "b", b);
    }

        @Test
    public void test_A_method_should_not_throw_exception()
            when(b. execute()).thenReturn(true); //just an example of a return value from b. execute()
            Boolean result = a.testHello();

        // Assert
        Assert.assertEquals(true, result);
    }
+1

Apex TestHelper . ( ) , . , , .

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; 
    }
}

}

+1

Source: https://habr.com/ru/post/1778869/


All Articles