Search code examples
unit-testingsalesforceapex-codevisualforce

How to unit test works in salesforce?


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

The result of testing MyClassOne would not return 100% test coverage because it says I have not covered MyClassTwo method2() part inside of MyClassOne file.

But I already wrote unit test for MyClassTwo inside of MyClassTwo file as you can see.

So does this mean I have to copy and paste the unit test in MyClassTwo file over to MyClassOne?

Doing so gives me 100% coverage but this seems really annoying and rediculous. Having same test in ClassA and ClassB....? Am I doing wrong or is this the way?

Having said, is it possible to create mock object in salesforce? I haven't figure how yet..

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

I re-wrote the code and updated above, this time for sure classOne test would not return 100% even though it is not calling classTwo method2()


Solution

  • Comments about Java mock libraries aren't very helpful in Salesforce world ;) At my projects we usually aimed for making our own test data in the test method, calling real functionality, checking the results... and whole test framework on Salesforce side is responsible for transaction rollback (so no test data is saved to DB in the end regardless whether the test failed or passed).

    Anyway...

    Masato, your classes do not compile (methods outside class scope, public String hello() without any String returned)... After I fixed it I simply right-clicked the MyClassA -> Force.com -> run tests and got full code coverage without any problems so your issue must lie somewhere else...

    Here's how it looks: http://dl.dropbox.com/u/709568/stackoverflow/masato_code_coverage.png

    I'm trying to think what might have gone wrong... are you sure all classes compile and were saved on server side? Did you put test methods in same classes as functionality or in separate ones (generally I make separate class name with similar name like MyClassATest). If it's a separate class - on which file did you click "run tests"? Last but not least - if you're facing this issue during deployment from sandbox to production, make sure you selected all classes you need in the deployment wizard?