简体   繁体   English

运行测试时,从类路径中排除src / main / resources

[英]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.xmlsrc/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

  • core-site.xml 核心的site.xml
  • hbase-site.xml HBase的-site.xml中
  • hdfs-site.xml HDFS-site.xml中

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.

相关问题 应用程序(src / main)运行时,类路径中的src / test / resources中的文件是? - Are files in src/test/resources on classpath while application (src/main) is running? 在 Eclipse 中为 spring 运行单元测试时将 src/main/resources 添加到类路径 - Add src/main/resources to classpath when running unit test in eclipse for spring src / main / resources中的文件不在classpath中 - file in src/main/resources is not in classpath 使用mvn exec:java时,如何将src / main / resources添加到类路径中 - How can I add src/main/resources to the classpath when using mvn exec:java Maven:不将文件从src / main / resources复制到类路径根目录-可执行Jar - Maven: Not copying files from src/main/resources to the classpath root - Executable Jar Maven项目不会将文件放在classpath的src / main / resources中 - Maven project doesn't put files in src/main/resources on classpath Mule - 在Studio和Standalone中运行时如何访问Java类中src / main / resources中的文件? - Mule - how to access files in src/main/resources in Java class when running in Studio and Standalone? 从/ src / main / resources加载文件 - Loading file from /src/main/resources 如何包含来自 src/main/java 的资源 - How to include resources from src/main/java 从src / main / resources中读取文件作为文件? - Reading file from src/main/resources as File?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM