title: Unit testing a Play controller using mocks
body: <p>The title pretty much says it all. I would like to set up a traditional JUnit test to mock a controller's dependencies and run tests against actions. </p>
  
  <p>I've found that I can achieve my goal like this:</p>
  
  <pre><code>public class AccountsControllerTest {
      private controllers.Accounts accountsController;
  
      @Test
      public void test() {
          running(fakeApplication(), new Runnable() {
              public void run() {
                  accountsController = new controllers.Accounts();
                  accountsController.setAccountsWorkflow(mock(workflow.Accounts.class));
              }
          });
      }
  }
  </code></pre>
  
  <p>The obvious problem here is that I'm instantiating my class under test and injecting mock dependencies from the test method itself, when I should be doing that in the <code>setup()</code> method. It seems that the <code>setup()</code> method is useless if I'm going to test my controller in a traditional way.</p>
  
  <p>Of course I can test controllers <a href="http://www.playframework.org/documentation/2.0/JavaFunctionalTest">the way Play recommends</a>, but my application is dependent on an external SOAP web service, so I need unit tests to show that our code is working when their servers are down.</p>
  
  <p>So, what's the best way to unit test a Play controller using mocks while still taking advantage of <code>setup()</code> and <code>teardown()</code> methods?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I realize I'm assuming some knowledge here, so for those who are unaware, controller instantiation in a unit test must be wrapped in a <code>running()</code> function or Play! will throw a runtime exception saying that no application has been started.</p>