如何模拟单元测试中缺乏网络连接 [英] how to simulate lack of network connectivity in unit testing

查看:135
本文介绍了如何模拟单元测试中缺乏网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般的问题是如何模拟(作为 JUnit 测试用例套件的一部分)缺乏网络连接,因为这在某些测试用例中是一个重要的考虑因素。有没有办法通过Java API(或通过JVM选项)这样做,以便某些测试用例可以在网络断开连接下运行? (模拟或真实?)。

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?).

更具体的情况(如果没有普遍适用的解决方案)是我正在做一堆XML文件处理(包括XSD) -validation)我需要确保没有通过网络获取任何内容,具体来说,没有使用 xsi:schemaLocation 属性值(提示),并且所有XSD实际上都是从类路径获得。我使用的是 Validator 使用自定义 LSResourceResolver 加载类路径中可能需要的任何XSD。

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.

我想我可以实现 resolveResource LSResourceResolver 的方法,以便永远不会返回 null (因此,永远不要回到打开与资源的常规URI连接的默认行为)但我不确定这是否足够,无论如何我会更自信如果我可以运行我的 JUnit 在模拟岛模式下测试(无需手动关闭我机器上的接口)。

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).

更新

接受的答案,即提供的 -DsocksProxyHost 方法我需要的解决方案,因为 JUnit任务可以接受VM参数(如果 fork 设置为 true )所以我可以在我的Ant文件中包含以下内容:

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"/>
    ...

...包裹在 contrib:if 所以我可以从命令行控制是否在网络连接条件下运行JUnit测试。

... wrapped inside contrib:if so I can control from the command line whether to run JUnit tests under conditions of network connectivity or not.

推荐答案

这不是我建议做的,但是......
你可以尝试使用安全管理器。默认情况下,JVM不使用安全管理器,通过设置 -Djava.security.manager ,您将激活
默认安全管理器。 AFAIR,安全管理器的默认行为是阻止任何连接(只要未在安全策略中明确授予权限)。但。每次连接被阻止时,您将获得 java.net.NetPermission 异常。您还可能遇到本地文件访问,反射调用等问题。

That's not what I would advise to do, but... you can try to use a security manager. By default JVM does not use a security manager, by setting -Djava.security.manager you will activate the default security manager. AFAIR, the default behaviour of the security manager is to block any connections (as long as the permission is not explicitly granted in security policy). But. You will get java.net.NetPermission Exception each time a connection will be blocked. You can also experience problems with local files access, reflection calls and so on.

另一种可能性是将网络代理设置为不存在的地址,例如 -DsocksProxyHost = 127.0.0.1 。然后任何TCP套接字都会尝试使用SOCKS服务器并失败。

Another possibility is to set the network proxy to a non-existent address, like -DsocksProxyHost=127.0.0.1. Then any TCP socket will try to use the SOCKS server and fail.

这篇关于如何模拟单元测试中缺乏网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆