I have a directory in my test folder with all my JavaScript file need for my tests.
src/main
src/test
src/test/java
src/test/javascript
src/test/ressources
I have some tests in src/test/java that use some resources in src/test/javascript.
It is well working when i launch Junit test manually. But when i launch the Maven test goal. i have this :
Couldn't read source file "src/test/javascript/envJsOptions.js": src\test\javascript\envJsOptions.js (The system cannot find the path specified)
not sure how you are using js file in junit test
but just try using javascript/envJSOptions.js
src/main/java
src/main/resources
src/test/java
src/test/resources
all these folders can be considered to be in classpath (same level)
so the paths will be started from that point on, if you are using a resource as stream..
Related
My JUnit tests need Java source code files as input. I've created those files under src/test/resources. I read a test resource file from src/test/resources (e.g., src/test/resources/files/SimpleTestClass.java via
final String expectedClass = new String(ClassLoader.getSystemClassLoader()
.getResourceAsStream("files/SimpleTestClass.java")
.readAllBytes());
The behavior that I expect is that the test resource Java files will not be compiled. This is exactly what Gradle does, therefore, running the tests with gradle test works fine.
Unfortunately, running the JUnit tests from within Eclipse results in a compilation of those test resource Java files. The bin folder now includes .class files instead of the .java files.
I could exclude the test resource Java files from the compilation. However, this will also disable Eclipse's Java editor functionalities for those files, e.g., error highlighting or refactoring.
For the test resource Java source code files: How can I keep the Java editor functionalitites in Eclipse, but exclude the compilation of those files so that they still are copied to the bin folder?
I have a JavaFX project that loads the FXML files using getClassLoader().getResource, for example:
Main.class.getClassLoader().getResource("fxml/App.fxml").
The main code runs fine but when I run tests with Maven Surefire Plugin I have this error:
java.lang.IllegalStateException: Location is not set.
javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
Upon further investigation I discovered that when getClassLoader().getResource() is called while executing the test, the path it tries to resolve is in "target/test-classes" folder, whereas the resources reside in "target/classes" folder. How do I solve this problem?
My project follows Maven's default structure if that is relevant.
Edit: added the line of code that does the resource loading
When running tests in Maven, the classpath is comprised of both target/classes and target/test-classes
target/classes is used to store compiled classes and resources (under src/main/java, src/main/resources)
target/test-classes is used to store compiled test classes and test resources (under src/test/java and src/test/resources respectively)
You don't show the code that doesgetClassLoader().getResource() and the most important is the parameter of this getResource - how does it get resolved in runtime (during the test), so its hard to say more, but probably the issue is that this parameter is not specified correctly.
For example, if its file src/main/resources/sample.xml it should be resolved as:
getClassLoader().getResource("/sample.xml")
I have a web app and i am trying to set up the config for the unit tests. I have the following structure
project
-src/main/java
-src/main/resources
-src/test/java
-src/test/resources
-src
-main
-webapp
-WEB-INF
-spring
-test
-spring
All my spring configuration files are stored in the project\src\main\webapp\WEB-INF\spring directory. But the issue is that my test configuration files are stored in the project\src\test\spring directory.
For my tests i want to use some of the configuration files in the project\src\main\webapp\WEB-INF\spring directory but i keep getting a file not found exception when i try access them.
Is there a way to keep my configuration files in the WEB-INF folder but still visible to my test configuration files?
Why do you keep configurations in project\src\main\webapp\WEB-INF\spring ?
You have to use:
src/main/resources
src/test/resources
respectively.
All files located in "src/main/resources" after maven build will be moved to {appRoot}/WEB-INF folder anyway.
Then from your tests you will see all files located in "src/main/resources"
I want to use in memory database when running mvn test and a file-backed database in development. I have filters "working" in that I run mvn resources:resources I get the templates in src/main/resources rendered correctly into the target/classes directory. Using Grizzly in my Main class, the webserver does pickup the hibernate.cfg.xml in target/classes.
However, when I run mvn test, it seems that the hibernate.cfg.xml is read as the raw template in src/main/resources rather than what is rendered in target/classes. How can I get mvn test (and running tests from intellij) to use the filtered/rendered resources?
put the test config file in src/test/resources so at test time it will take precedence (in classpath)
I currently started working on a maven web-app project that needs to be launched with the jetty:run-exploded goal for development/debugging in eclipse.
Now, I have an XML file which contents I need to access at runtime. My problem is: where to put the file so that the code that does the reading works both in "exploded" and packaged (i.e. in the WAR) mode?
Putting the file in src/main/java (so as to be in the classpath) won't cut it since maven filters out all non-java files on packaging.
When the file is in src/main/resources, one mean would be to figure out the root path of the project (during eclipse development) and look into that directory - but this won't be the case anymore when the project will be packaged.
Of course I could go into writing code that tries to read the file from both locations, but this seems rather cumbersome. Any suggestions?
Files in src/main/resources are copied to the target/classes directory and are available on the class path. Just read them from the class path. As explained in How do I add resources to my JAR? from the maven documentation (with a test resource here):
In a unit test you could use a simple
snippet of code like the following to
access the resource required for
testing:
...
// Retrieve resource
InputStream is = getClass().getResourceAsStream("/test.properties" );
// Do something with the resource
...
In such case I put the file under src/main/resources directory and use Spring's ClassPathResource. This way the file is accessible in IDE, during Maven build process and in runtime.