[英]Exclude src/main/resources from classpath when running tests
I have a local integration test that uses the HBaseTestingUtility
to create a local HBase test environment. 我有一个本地集成测试,该测试使用HBaseTestingUtility
创建本地HBase测试环境。 Starting it with its default configuration is as simple as: 使用其默认配置启动它很简单:
HBaseTestingUtility hbaseUtility = new HBaseTestingUtility();
hbaseUtility.startMiniCluster();
and then I have several JUnit tests that interact with HBase. 然后我有几个与HBase交互的JUnit测试。
However, if my src/main/resources
folder contains hbase-site.xml
then the HBase instance will pull in Kerberos configuration from there, try to connect to a remote HBase instance, and die. 但是,如果我的src/main/resources
文件夹包含hbase-site.xml
则HBase实例将从那里拉入Kerberos配置,尝试连接到远程HBase实例,然后消失。 I don't understand why this happens - I thought that only src/test/resources
files were available when running tests? 我不明白为什么会这样-我认为在运行测试时只有src/test/resources
文件可用吗?
How can I exclude src/main/resources
from the classpath when running these tests? 运行这些测试时,如何从类路径中排除src/main/resources
?
If it helps, my build tool is Gradle, and there's no configuration in it that would tell tests to read from src/main/resources
. 如果有帮助,我的构建工具是Gradle,并且其中没有配置可以告诉测试从src/main/resources
读取。
Ultimately your tests will be testing everything which gets included in the jar/war produced by the project. 最终,您的测试将测试项目所产生的jar / war中包含的所有内容。 This includes compiled classes and resources. 这包括已编译的类和资源。 So it makes perfect sense to me that src/main/resources
are on the test classpath. 因此,对我来说, src/main/resources
位于测试类路径上是非常有意义的。
Rather than excluding specific files from src/main/resources
I suggest that you move hbase-site.xml
from src/main/resources
to src/non-test/resources
or similar. 我建议您将hbase-site.xml
从src/main/resources
移到src/non-test/resources
或类似文件中,而不是从src/main/resources
排除特定文件。 You could then include this folder in the jar but leave it out of the test classpath 然后,您可以将此文件夹包含在jar中,但将其保留在测试类路径之外
A workaround for this specific situation (using HBaseTestingUtility
) is to create blank files for each of the Hadoop XML files that exist in src/main/resources
. 针对这种特定情况的一种解决方法(使用HBaseTestingUtility
)是为src/main/resources
中存在的每个Hadoop XML文件创建空白文件。 In my case, I added the following files to src/test/resources
: 就我而言,我将以下文件添加到src/test/resources
:
Each containing: 每个包含:
<configuration>
</configuration>
The blank files take precedence over those in src/main/resources
, and the HBaseTestingUtility
works as expected. 空白文件优先于src/main/resources
,并且HBaseTestingUtility
可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.