Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've nearly got it. The problem is that the <a href="http://c2.com/cgi/wiki?ClassUnderTest" rel="nofollow">Class Under Test</a> (<strong>CUT</strong>) is not built for unit testing - it has not <em>really</em> been <a href="http://en.wikipedia.org/wiki/Test-driven_development" rel="nofollow">TDD</a>'d.</p> <p>Think of it like this&hellip;</p> <ul> <li>I need to test a function of a class - let's call it <em>myFunction</em></li> <li>That function makes a call to a function on another class/service/database</li> <li>That function also calls another method on the <strong>CUT</strong></li> </ul> <p>In the unit test</p> <ul> <li>Should create a concrete <strong>CUT</strong> or <code>@Spy</code> on it</li> <li>You can <code>@Mock</code> all of the other class/service/database (i.e. external dependencies) </li> <li>You <em>could</em> stub the other function called in the <strong>CUT</strong> but it is not really how <em>unit</em> testing should be done</li> </ul> <p>In order to avoid executing code that you are not strictly testing, you could abstract that code away into something that can be <code>@Mock</code>ed.</p> <p>In this <em>very</em> simple example, a function that creates an object will be difficult to test</p> <pre><code>public void doSomethingCool(String foo) { MyObject obj = new MyObject(foo); // can't do much with obj in a unit test unless it is returned } </code></pre> <p>But a function that uses a service to get MyObject is easy to test, as we have abstracted the difficult/impossible to test code into something that makes this method testable.</p> <pre><code>public void doSomethingCool(String foo) { MyObject obj = MyObjectService.getMeAnObject(foo); } </code></pre> <p>as MyObjectService can be mocked and also verified that .getMeAnObject() is called with the <em>foo</em> variable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload