Ben Watson
Ben Watson

Reputation: 5531

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

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. I don't understand why this happens - I thought that only src/test/resources files were available when running tests?

How can I exclude src/main/resources from the classpath when running these tests?

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.

Upvotes: 2

Views: 1595

Answers (2)

Ben Watson
Ben Watson

Reputation: 5531

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. In my case, I added the following files to src/test/resources:

  • core-site.xml
  • hbase-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.

Upvotes: 0

lance-java
lance-java

Reputation: 27994

Ultimately your tests will be testing everything which gets included in the jar/war produced by the project. This includes compiled classes and resources. So it makes perfect sense to me that src/main/resources are on the test classpath.

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. You could then include this folder in the jar but leave it out of the test classpath

Upvotes: 2

Related Questions