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)
Related
I have a Java Spring application with unit tests in the directory
src/test Specifically src/test/com/client/rest
I'd like to add a child directory here, something like
src/test/com/client/rest/controllers
Which contains the unit tests for all controllers of the application. I created a file in this directory with a "#Test" end-point, but did not do anything to POM.xml. When I run mvn clean package I get the following error
The goal you specified requires a project to execute but there is no POM in this directory [path]. Please verify you invoked Maven from the correct directory.
I tried adding this new directory as a testResource element under build in POM.xml, but that did not work. Prior to adding this new file, the tests ran fine with <testSourceDirectory>src/test</testSourceDirectory> under the build element.
How do I properly integrate this new JUnit directory into Maven?
EDIT: I moved my new file to the standard parent directory where the other JUnit test files are (src/test/com/client/rest) and it seemed to register fine. What is the best practice for storing JUnit class files, and is it worthwhile to try to create new child directories for organizational purposes?
I think the error comes from just running the mvn command from somewhere other than your project root.
How do I properly integrate this new JUnit directory into Maven?
Follow the Maven Standard Directory Layout - https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
Your tests should be under src/test/java/com/client/rest/controllers . They should then be discoverable and just work without any additional Maven configuration.
What is the best practice for storing JUnit class files, and is it worthwhile to try to create new child directories for organizational purposes?
It is worth organizing your tests. I think the piece you're missing is that you are writing tests in Java so you need to organize your tests with both directories and packages. So a test located under src/test/java/com/client/rest/controllers would have a package of com.client.rest.controllers .
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 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..
I have a .jar file, where I have packaged my maven managed java project which has few Junit test cases as well. Now I want to run test cases from my .jar file. For example, from the source code mvn test will run the test cases, but it is not working for .jar file.
How do I run Junit test cases from my .jar file?
You need to configure the maven-jar-plugin, and include the test classes in the jar
http://maven.apache.org/plugins/maven-jar-plugin/usage.html
have a look also here
http://maven.apache.org/plugins/maven-jar-plugin/test-jar-mojo.html
Should I expect files in main/resources to be on the classpath when running the maven jetty plugin? Meaning will they be available to the app I'm running inside jetty? Will I be able to load them as classpath resources rather than via the file system?
The same question goes for running junit tests inside Eclipse, using the Eclipse Maven plugin.
If this directory is not on the classpath, can I add it?
The resources from src/main/resources are copied into target/classes during the process-resources phase so, yes, they will be on the class path when running the maven jetty plugin. They will also be available when running unit tests (actually, see this previous answer). This applies inside Eclipse and on the command line.