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?
Related
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'm trying to find more information on how eclipse handles Running a project.
I want to understand it more because I have an issue I'm currently having where apache-poi .jar files which have been included into the classpath of my project will work properly when the project is ran through eclipse, but will not be detected when going to the same projects folder and running the main jar file to start the program.
It gives me the error: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell (although sometimes instead of Cell, it's Sheet)
What could I consult to understand what is going on here, and possibly solve this issue?
Your NoClassDefFoundError indicates that the library was not available while running the jar.
This depends upon how you are exporting your project into the jar file.
If you're using eclipse to do so, you can:
Export->Java->Runnable Jar to create a jar that includes its dependencies
Make sure to check Package required libraries into generated JAR.
This will make all your jars (in build path including apache-poi.jar) as a part of the final jar.
It runs from eclipse because libraries are on the build path of the eclipse which makes them available in the classpath.
I am trying to build a Runnable jar file using the Eclipse>Export>Runnable Jar File option.
Things are fine except that it is also exporting my test classes
I am only selecting classes from my src/java folder and not anything from src/test folder
One way out is to add the MANIFEST file later after building as a normal jar. But would like to know if there is a way to prevent tets classes from being exported
In the Java Build Path Source menu add the main and test sources separately. Now in the Deployment Assembly menu remove the test sources.
Is that what you're looking for?
have a look here
I'm using cobertura to find out code coverage of my test suite, which tests a java based solution bundled through a jar file, cobertura cmd line reference
I have done the instrumentation part and obtained the instrument class files.
For the second step, I'm not sure whether I need to re-bundle the instrumented class files into a jar file since my test suite uses this jar file or can I simply set my CLASSPATH variable to include the instrumented class file while continue to use original jar file (which contains the uninstrumented class files).
Thanks.
You have to include your instrumented files in the classpath BEFORE any other containers/Folders that may include the same compiled files.
In you case before the jar file with the instrumented files.
You don't need to rebundle.
Another possibility would be to instrument the jar file directly.
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.