Note that there are some explanatory texts on larger screens.

plurals
  1. POMockito: Mocking "Blackbox" Dependencies
    text
    copied!<p>So I have been asked to read up on mocking and BDD for our development team and play around with mocks so as to improve a handful of our existing unit tests (as an experiment).</p> <p>I have ultimately chosen to go with Mockito for a number of reasons (some outside the scope of my control), but namely because it supports both stubbing and mocking for instances when mocking would not be appropriate.</p> <p>I have spent all day learning about Mockito, mocking (in general) and BDD. And now I am ready to dig in and start augmenting our unit tests.</p> <p>So we have a class called <code>WebAdaptor</code> that has a <code>run()</code> method:</p> <pre><code>public class WebAdaptor { private Subscriber subscriber; public void run() { subscriber = new Subscriber(); subscriber.init(); } } </code></pre> <p><strong>Please note:</strong> I do not have a way to modify this code (for reasons outside the scope of this question!). Thus I do <em>not</em> have the ability to add a setter method for <code>Subscriber</code>, and thus it can be thought of as an unreachable "blackbox" inside of my <code>WebAdaptor</code>.</p> <p>I want to write a unit test which incorporates a <code>Mockito</code> mock, and uses that mock to <code>verify</code> that executing <code>WebAdaptor::run()</code> causes <code>Subscriber::init()</code> to be called.</p> <p>So here's what I've got so far (inside <code>WebAdaptorUnitTest</code>):</p> <pre><code>@Test public void runShouldInvokeSubscriberInit() { // Given Subscriber mockSubscriber = mock(Subscriber.class); WebAdaptor adaptor = new WebAdaptor(); // When adaptor.run(); // Then verify(mockSubscriber).init(); } </code></pre> <p>When I run this test, the actual <code>Subscriber::init()</code> method gets executed (I can tell from the console output and seeing files being generated on my local system), <em>not</em> the <code>mockSubscriber</code>, which shouldn't do (or return) anything.</p> <p>I have checked and re-checked: <code>init</code> is <code>public</code>, is neither <code>static</code> or <code>final</code>, and it returns <code>void</code>. According to the docs, Mockito should have no problem mocking this object.</p> <p>So it got me thinking: do I need to explictly associate the <code>mockSubscriber</code> with the <code>adaptor</code>? If this is a case, then ordinarily, the following would normally fix it:</p> <pre><code>adaptor.setSubscriber(mockSubscriber); </code></pre> <p>But since I cannot add any such setter (please read my note above), I'm at a loss as to how I could force such an association. So, several very-closely-related questions:</p> <ul> <li>Can anyone confirm that I've set the test up correctly (using the Mockito API)?</li> <li>Is my suspicion about the missing setter correct? (Do I need to associate these objects via a setter?)</li> <li>If my above suspicion is true, and I can't modify <code>WebAdaptor</code>, are there any circumventions at my dispose?</li> </ul> <p>Thanks in advance!</p>
 

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