title: How to unit test works in salesforce?
body: <p>I've done writing code on salesforce and in order to release the unit tests have to cover at least <strong>75%</strong>.</p>
  
  <p>What I am facing is that the <code>classOne</code> that calls methods from <code>classTwo</code> also have to cover <code>classTwo</code>'s unit test <em>within classOne</em> even though it is done in <code>classTwo</code> file already.</p>
  
  <p><strong>File MyClassTwo</strong> </p>
  
  <pre><code> 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
      }
  
  }
  </code></pre>
  
  <p><strong>File MyClassOne</strong> </p>
  
  <pre><code> 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');
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>But I already wrote unit test for MyClassTwo inside of MyClassTwo file as you can see.</p>
  
  <p>So does this mean I have to copy and paste the unit test in MyClassTwo file over to MyClassOne?</p>
  
  <p>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?</p>
  
  <p>Having said, <strong>is it possible to create mock object in salesforce?</strong> I haven't figure how yet..</p>
  
  <p><a href="http://sites.force.com/answers/ideaView?c=09a30000000D9xt&amp;id=087300000007m3fAAA&amp;returnUrl=/apex/ideaList%3Fc%3D09a30000000D9xt%26category%3DApex%2B%2526%2BVisualforce%26p%3D19%26sort%3Dpopular" rel="nofollow">http://sites.force.com/answers/ideaView?c=09a30000000D9xt&amp;id=087300000007m3fAAA&amp;returnUrl=/apex/ideaList%3Fc%3D09a30000000D9xt%26category%3DApex%2B%2526%2BVisualforce%26p%3D19%26sort%3Dpopular</a></p>
  
  <p><strong>UDPATE</strong></p>
  
  <p>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() </p>