how to simulate lack of network connectivity in unit testing



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 implemented the resolveResource method of the LSResourceResolver so as to never return null (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).


No comments:

Post a Comment