Configuring JUnit Directory Structure in Maven - java

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 .

Related

Unit test with resources loaded with ClassLoader and Maven Surefire

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")

Packaging spring sub-projects into one jar

I'm having Spring based web-app that I'm trying to bundle into a single jar.
My thoughts on laying out the project was to have a root project that contains multiple sub-projects, where there would be one sub-project that is a spring-boot application, and others sub-projects maybe my own-written code handle certain business logic let's say.
So the structure may look something like:
/root
build.gradle <- the problem is here, how I should write root project build script
/spring-backend-subproject
/other-business-subproject
Having all these sub-projects, I want to package them all into one single JAR, so that I can just do
java -jar build/libs/RootJar.jar
otherwise I need to run:
./gradlew bootRun
which will run the particular main class in the "spring-boot" sub-project
I've been searching online for how to bundle all subproject jars into one jar, such as this one: Can Gradle jar multiple projects into one jar?
but I often just end up having errors such as main-class not found(which does not happen if I run the jar specifically in the "spring sub-project" folder) or SpringApplication class not found (again does not happen if run jar in the sub-project folder)
How may I solve this problem?
Thanks!
I use this pattern extensively. Instead of trying to put the deployment classes into the root, add a module (I usually call it -launcher) that lists the other modules as dependencies and contains your main class and related application code, such as any application.properties you may have.
This module will produce an artifact my-project-launcher.jar, and you deploy this to whatever platform you're using.

Where should I save TestNG configuration test files

I have a question about the TestNG configuration files and Maven project structure, I would like know the best practices to save the configuration files.
E.g. I have use a testing.xml is a file which uses several classes to create a Suite. If I use the java application archetype of maven, where is the best place to save the xml file (testing.xml)?. I have taken a look to Maven standar directories, but I do not find anything about this issue.
src/test/resources
you can create a folder test-suites here and have different suite xml's here.

Understanding of usage of Maven folder structure

Once we create maven project, it creates folder structure like following:
I'm planning to put my Selenium tests in src/test/java folder and resources like test data, properties etc in src/test/resources. Whatever I'm following here is correct?
If I follow above scenario, there is no use of src/main/java and src/main/resources, would be fine if I remove them from folder?
Or let me know how can I use that folder structure in better way for Selenium tests.
There is one more folder src in the project. What is that for? How can I make use of that folder?
In your project there is only one src folder, you can see it if you go to the folder directly with the browser, without using Eclipse.
What you see that structure/view is:
The source folders (Source folders are a way to cut down on a
project's indexing scope. You can mark the folders that are part of
your day to day work or part of a subsystem that you work on. All
files inside source folders will be indexed and are, thus,
searchable. Note that any files pulled in by a file inside a source
folder will also be indexed.)
Dependencies: JRE and Maven dependencies
The physical folder of src: Inside it there is the same content of the source folders, but
with folder structure, because there are same things.
Binaries and pom.xml
About the selenium case, it depends. You can create a Selenium folder in your main project into src/test/java, and put the tests there (you do not need another project) or put it another project as you said.
Basically Maven would help you to build a standard project directory structure for the type of "archetype" you have chosen. So the kind of project you created looks like a generic java project which will have a source code folder and resources folder. Corresponding to that, it would also have provision for writing unit tests during the development phase. So the folders containing "test" would hold those unit tests in form of java classes and required resources.
In your case, since you don't need the java part of the project, you could leave them empty rather than removing them. May be in future you would need to "stub" some functionality and you would use those folders.
You can explore the list of archetypes at below given link and check if any archetype exist for Simply test projects.
[http://docs.codehaus.org/display/MAVENUSER/Archetypes+List][1]

Maven, different database url for test and dev

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)

Categories

Resources