user1071914
user1071914

Reputation: 3403

How can I find the classpath that a Surefire/Junit4 test will search?

I need to override some properties in a JUnit test running as part of our Maven-based build. In order to do that, I need to figure out where to place the override file in our test environment. How can I determine the classpath that this line is checking when it runs in the test environment?

stream = getClass().getClassLoader().getResourceAsStream(resourceName);

I've checked the javadoc for ClassLoader but it's not terribly clear on this point.

Upvotes: 3

Views: 3435

Answers (2)

Foumpie
Foumpie

Reputation: 1555

How about debugging the output ?

mvn --debug clean install

This will output pretty much everything you need. Inlcuding the classpath in the order it is loaded.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Determined Maven Process ID 15288
[DEBUG] boot classpath:  
\<REPO>\org\apache\maven\surefire\surefire-booter\2.21.0\surefire-booter-2.21.0.jar  
\<REPO>\org\apache\maven\surefire\surefire-api\2.21.0\surefire-api-2.21.0.jar  
\<REPO>\org\apache\maven\surefire\surefire-logger-api\2.21.0\surefire-logger-api-2.21.0.jar  
\<PROJECT>\src\target\test-classes  
\<PROJECT>\src\target\classes  
\<REPO>\<all your dependencies> 
\<classpath configured as additionalClasspathElement>  
\<REPO>\org\apache\maven\surefire\surefire-junit4\2.21.0\surefire-junit4-2.21.0.jar

boot(compact) classpath:  
<exact same thing as above without full file paths>

Forking command line: 
cmd.exe /X /C ""C:\Program Files\Java\jdk1.8.0_121\jre\bin\java" -ea -jar C:\<TEMP>\surefire3859643060701954342\surefirebooter6402473409535798561.jar C:\<TEMP>\surefire3859643060701954342 2018-06-04T11-50-05_916-jvmRun1 surefire7765693984737889544tmp surefire_08839138260978763550tmp"

Running <all my test cases>

Upvotes: 2

Matthew Farwell
Matthew Farwell

Reputation: 61705

To find the classpath, you can simply use the dependency:build-classpath plugin of maven:

mvn dependency:build-classpath -DincludeScope=test

This will give you the full classpath that it uses when running surefire. Actually, includeScope=test is the default, so you can just miss it out. Otherwise, you can specify any scope, for instance compile:

mvn dependency:build-classpath -DincludeScope=compile

Upvotes: 5

Related Questions