Gradle build not invoking JUnit - gradle

My gradle script for building an EAR is not running any JUnit tests I have. The command I used for invoking the script is
gradle build
I also tried
gradle test
and that also did not work.
My build structure is as follows
settings.gradle
gradle.properties
EARProject
build.gradle
META-INF
application.xml
lib
JARProject
build.gradle
src/main/java
....
src/test/java
....
WebProject
build.gradle
src/test/java
src/main/java
WebContent
.....
I am invoking the gradle command from EARPRoject directory.
What changes I need to do in order to run the test cases. Please note that if I run the gradle build command from individual projects it is working as expected.
Regards
-Albin

Apparently, EARProject doesn't have any tests. Depending on what you want, run gradle build from the top directory, or from a subproject/subdirectory containing tests. Alternatively, you can run gradle buildNeeded from EARProject, which will perform a full build of all projects that EARProject depends upon (which presumably includes JARProject and WebProject).

Related

Can I run IntelliJ unit tests in a maven project without re-running maven goals?

I have a project with a deep maven structure and am evaluating switching from Eclipse to IntelliJ.
I've imported the top-level pom.xml. I know that I can:
Build the whole project with maven goals (I use 'test-compile' to build the test classes)
Run the tests from the IntelliJ test runner. This seems to find the classes/test-classes from the target folder of the project.
However, I would like to just change a test or the code under test, save it, and rerun it without having to manually run the maven test-compile again (like I could with Eclipse). Is this possible with IntelliJ?
I think the issue is that the IntelliJ 'Build Project' action does not build the test classes (and other classes) into the target folder that the test runner looks in.
If you import a Maven project in IntelliJ IDEA, you will be able to build and run the tests from the IDE instead of Maven which is a lot faster because of the incremental compilation.
Make sure this option is disabled and use JUnit Run/Debug configuration, don't run the maven goals.
Thanks to the suggestion from #CrazyCoder, I was able to get this working by following these steps, as I think the issue was Eclipse artifacts fighting with IntelliJ artifacts:
Remove all .classpath, .project and .iml files from the entire code tree
Remove .idea from the root - we need to start again
Open the top-level pom.xml in IntelliJ
If needed, perform a mvn compile (needed for me due to some generated sources)
Rebuild your project
After this, don't open your project in Eclipse again!

Run testng.xml from command prompt. I have jar that is created from Maven Project. Surefireplugin is not solving my purpose

What i have
Jar created from maven project
TestNG.xml file
What i want
Want to run testng.xml from command prompt
What i tried
Used surefire plugin in pom then executed mvn test command.
What is problem then ?
Surefire plugin will need. src folder and every time it will build project.
Any solution available ? Using jar file and testng . I dont want to use src or bin
You could use a multi-module maven project.
For example with two modules, A and B. Module A will build the jar file, and in module B, you can specify A as a dependency, and run your tests from there.

In what lifecycle point are tests ignored when building?

I am trying to understand the maven build lifecycle when it is regarding to tests:
Let's say I have a project with a class in the src/main folder and another one in the src/test. When I run mvn install, it get's compiled, then tested, then packaged and installed.
Until now I thought that maven would not package the tests in the snapshot, because this is not "production" code. Is this true?
In this case, when does maven ignores the test files?
What you said about the Maven lifecycle is true. The "main" phases include:
compile: in this phase, the maven-compiler-plugin:compile goal is run which compiles the main code of the project.
test-compile: in this phase, the maven-compiler-plugin:testCompile goal is run which compiles the test code of the project.
test: in this phase, the maven-surefire-plugin:test goal is run and all the unit tests are executed.
package: in this phase, the maven-jar-plugin:jar goal is run and creates a JAR of the current project. By default, this JAR will include all classes under target/classes, which contains all the main classes, but not the test classes which are located inside target/test-classes. You could say that it is at this step that Maven ignores the test classes.
install: in this phase, the maven-install-plugin:install will install the main artifact, and every attached artifact, into your local repository.
To answer more precisely, it's not that Maven ignores the test classes, it is that by default, the packaging process only considers the main classes. But you can configure it to also package the test classes with the maven-jar-plugin:test-jar goal inside another attached artifact.

Can we use maven surefire plugin to execute testcases present in the source directory

The Java project I am working on is a list of testcases written in Java for testing C++ code. So I want to run the testcases from the src directory in the test phase of the maven lifecycle. How do I configure the maven surefire plugin to achieve this.
Since it is Java based tests that presumably will not be part of the production code, I suggest that you follow Maven's standard directory layout and move the tests to the src/test/java directory as usual (otherwise, the tests will be part of the binary file if someone executes mvn package).

Compiling with gradle idea

Hi) when I compile the project are with gradle idea, I should get jar file...?
maybe in the folder dist...
The problem is that I get only two files start.sh and start.cmd
gradle idea doesn't compile the project. It creates project files (*.iws, *.ipr, *l.iml) for IDEA (the IDE from JetBrains). Likewise, there is gradle eclipse to create project files for the Eclipse IDE.
To create a Jar, you can do gradle jar or gradle build (assuming you have the java plugin applied). gradle tasks shows which tasks are available for a given project.
start.sh and start.cmd sound like they are coming from the application plugin. Are you using the application plugin?
The above poster is right that gradle idea simply creates the IntelliJ files that define your modules, src locations, etc. It does NOT compile the project.
Adding apply plugin: 'java' to your build.gradle will allow you to run gradle jar to generate a jar file.

Resources