title: how to simulate lack of network connectivity in unit testing
body: <p>The general question is how to simulate (as part of a <code>JUnit</code> suite of test cases) lack of network connectivity as this is an important consideration in some test cases. Is there a way to do so via a Java API (or via a JVM option) so that certain test cases can be run under network dis-connectivity? (simulated or real?).</p>
  
  <p>The more specific case (if there is no generally applicable solution) is that I am doing a bunch of XML-file processing (including XSD-validation) and I need to ensure that nothing is fetched over the network, specifically, that the <code>xsi:schemaLocation</code> attributes values (hints) are not used and that all XSDs are actually obtained from the classpath. I am using a <a href="http://docs.oracle.com/javase/7/docs/api/index.html?javax/xml/validation/Validator.html" rel="nofollow">Validator</a> with a custom <a href="http://docs.oracle.com/javase/7/docs/api/index.html?org/w3c/dom/ls/LSResourceResolver.html" rel="nofollow">LSResourceResolver</a> that loads any XSDs that may be needed from the classpath.</p>
  
  <p>I guess I could implement the <code>resolveResource</code> method of the <code>LSResourceResolver</code> so as to never return <code>null</code> (and therefore, so as to never fall back on the default behavior of opening a regular URI connection to the resource) but I am not sure whether that would be sufficient and at any rate I would feel more confident if I could run my <code>JUnit</code> tests in a simulated island-mode (without manually bringing down the interfaces on my machine).</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The accepted answer, namely the <code>-DsocksProxyHost</code> approach provided exactly the solution I needed as the <a href="http://ant.apache.org/manual/Tasks/junit.html" rel="nofollow">JUnit task</a> can accept VM parameters (if <code>fork</code> is set to <code>true</code>) and so I can have the following in my Ant file:</p>
  
  <pre><code>&lt;junit printsummary="true" showoutput="true" fork="true" maxmemory="256m"&gt;
      &lt;jvmarg value="-DsocksProxyHost=127.0.0.1"/&gt;
      ...
  </code></pre>
  
  <p>... wrapped inside <code>contrib:if</code> so I can control from the command line whether to run JUnit tests under conditions of network connectivity or not.</p>