The general question is how to simulate (as part of a JUnit
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?).
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 xsi:schemaLocation
attributes values (hints) are not used and that all XSDs are actually obtained from the classpath. I am using a Validator with a custom LSResourceResolver that loads any XSDs that may be needed from the classpath.
I guess I could implement the resolveResource
method of the LSResourceResolver
so as to never return null
(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 JUnit
tests in a simulated island-mode (without manually bringing down the interfaces on my machine).
UPDATE
The accepted answer, namely the -DsocksProxyHost
approach provided exactly the solution I needed as the JUnit task can accept VM parameters (if fork
is set to true
) and so I can have the following in my Ant file:
<junit printsummary="true" showoutput="true" fork="true" maxmemory="256m">
<jvmarg value="-DsocksProxyHost=127.0.0.1"/>
...
... wrapped inside contrib:if
so I can control from the command line whether to run JUnit tests under conditions of network connectivity or not.