Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2><a href="http://en.wikipedia.org/wiki/Mock_object" rel="nofollow noreferrer">Stubbing</a></h2> <p>You'll need a test double (stub) to allow isolated, easy, unit testing. The following is non tested, but demonstrates the idea. The use of <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow noreferrer">Dependency Injection</a> will allow you to inject at test time, a test version of your HttpURLConnection. </p> <pre><code>public class MyClass() { private IHttpURLConnection httpUrlConnection; public MyClass(IHttpURLConnection httpUrlConnection) { this.httpUrlConnection = httpUrlConnection; } public String pushEvent(Event event) { String url = constructURL(event); String response = callWebsite(url); return response; } } </code></pre> <p>Then you create a stub (sometimes referred to as a mock object) to be the stand in for the concrete instance.</p> <pre><code>class TestHttpURLConnection : IHttpURLConnection { /* Methods */ } </code></pre> <p>You'll also construct a concrete version, for your production code to use.</p> <pre><code>class MyHttpURLConnection : IHttpURLConnection { /* Methods */ } </code></pre> <p>Using your test class (an <a href="http://en.wikipedia.org/wiki/Adapter_pattern" rel="nofollow noreferrer">adapter</a>) you are able to specifiy what should happen during your test. A mocking framework will enable you to do this with less code, or you can manually wire this up. The end result of this for your test is that you'll set your expectations for your test, for example, in this case you may set OpenConnection to return a true boolean (This is just an example by the way). Your test will then assert that when this value is true, the return value of your PushEvent method matches some expected result. I've not touched Java properly for a while, but here are some <a href="https://stackoverflow.com/questions/22697/whats-the-best-mock-framework-for-java">recommended mocking frameworks</a> as specified by StackOverflow members.</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