IntelliJ fails on every test on a maven imported project - java

I imported a maven project to intellij with two modules under a parent project.
The test I want to run is made with TestNg and when I run it I just get a single line of:
java.lang.NullPointerException
No stacktrace, nothing.
If I run it from the command line it works, but only if I cd into the directory of one of the modules.
So basically thats the command that actually works:
cd server
mvn -Dtest=ConversionServiceT2 test
I'm pretty sure I didn't configure IntelliJ in the right way, but I have no idea where to start.
Here a few infos that might help:
I use the openJDK 11
I tried to set the working directory of the test config in IntelliJ to the server directory, which results in the same error
So how can I get this test (or tests in general) to run?

Related

IntelliJ does not recompile/build maven project

I am running into problems when using IntelliJ for Spring development. At first, the IntelliJ build command works fine and picks up changes I make to the source code. At some point, this stops working and I can only compile my sources through maven, as the build or rebuild project commands seem to do nothing anymore.
I am confused, as it works as desired at the start but then stops working at some point.
Steps to recreate:
Create a new Spring project using Spring Initializr:
curl https://start.spring.io/starter.zip -d type=maven-project -d javaVersion=11 -o demo.zip && unzip demo.zip
Start IntelliJ from the command line in that folder, this will auto-import the project: idea64 .
Write a log message in the main class and run the project using Run->Run 'DemoApplication'. This compiles the sources to the target folder and starts up the project. You should see the log message
Alter the log message, run again and the changes should be visible (so far so good)
Now re-compile the sources via maven command mvn compile and restart IntelliJ
From now on, running build or rebuild project within IntelliJ will not pick up any changes I make to any of my source files. In fact both commands don't seem to do anything at all. I have to compile via maven in order to properly compile my sources
I tried a few things to overcome this problem, but without success:
Invalidated caches/restart
Checked project settings for Project Compiler Output and set it to <project-folder>/target
Checked project settings/modules for output path and test output path and put in the path as above
Used Reimport All Maven Projects
Deleted the .idea folder within the project folder. This seems to work, but is very inconvenient and surely no intended behavior. Also it breaks again if I recreate the steps detailed above.
Is anyone experiencing similar problems and/or has a solution?
So for anyone running into the problems I described above:
Apparently deleting the .idea folder and restarting IntelliJ does indeed do the trick. The folder is recreated on restart and afterwards I'm able to switch between compiling within IntelliJ using run, build or rebuild project and compiling through maven goals (mvn compile, mvn test, mvn surefire:test, ...). This persists through restarts of IntelliJ such that I don't have to repeat this everytime I open a project.
I still don't know why this behavior happens, but it's a solution I'm happy to work with as long as I only have to do it every now and then for a project.
Sounds like do don't want Intellij's default build. You want Intellij to forward the build command to mvn. I assume this because you do not mention this step in your question.
Here's a screenshot that shows how to enable it:
More detail is available in the Intellij documentation https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#delegate_run_action

How to Run Eclipse+Maven+JUnit tests in Command Line

When I run mvn clean test, it gives me the following error:
'mvn' is not recognized as an internal or external command, operable program or batch file."
This is the link of my project.
I can run the tests by right-clicking on the Runner.java => Run as => JUnit Test. I can also run the tests from the JUnit 4 Test Explorer Tab.
I have read some articles where it's suggesting to set the PATH for Maven but all of them are about the case where you are installing Maven separately. I am not sure what PATH do I need to set when Maven is coming with Eclipse.
I did not install Maven separately, it came with my Eclipse version.
My Eclipse Version :
Eclipse IDE for Java Developers
Version: 2019-03 (4.11.0)
Build id: 20190314-1200
Some IDEs (IntelliJ for example) have their own version of maven for internal purpose. But it doesn't mean that's able to be used by the command line. If you want to compile your project using mvn clean install/package/deploy/test/etc you must install maven in your machine and set de VARs in the OS.
I guess Eclipse has the same behavior that Intellij, so please also install maven in your machine
This error means that Windows cannot find an executable program called "mvn". This means it is not on your path/current directory.
When you right click and run your junit tests, it invokes it using the JVM, using the java command. I suspect if you type "java -v" into CMD, it will not give you the same error.
Since Windows does not know where to look for maven, you must tell it implicitly, by setting your path to the directory containing the mvn executable. You can lookup "eclipse bundled maven default location" to find this, although it might be worth downloading it separately from the Apache website, and manually specifying the path.

Running JUnit tests together with Java project

I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time.
Is there a way to first run the JUnit and only if there were no test error run the project itself ? I want them to run together with 1 click (NOT run them seperately/manually).
Also, i would like the above to work even when the project is packed as a .jar file.
How can it be done ?
Thanks !
In Intellij:
Run -> Edit Configurations
Create a JUnit configuration for your tests
Create a Run configuration for your project.
And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1.
It doesn't matter how your project is packed (jar, ...)
Normally this is done by using a build management tool like: maven, gradle, ant. In this way the build tool will run tests for you and stop if they fail.
With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code.
See example project: https://github.com/HaveACupOfJava/maven-jar-demo

How to run cucmber test from command line and in jenkins

i have created a cucumber framework using Java and Maven. i execute cukesRunner.java locally in eclipse. now i wanted to execute it command line and thru jenkins. could some one please guide me.
cd to your project direct (the one containing pom.xml) and type
mvn test
Your runner class ends in Test, right? E.g. RunCukeTest.java

Java run time error when running the jar created from Maven (3.0.5) build using ess_es_server, ess_japi jar

I have a strange issue; I'm trying to run the jar created from maven build thru command prompt, but it gives run time error. When I'm running this from eclipse, it runs fine. Also, build is successful. Here's what I'm doing:
Created a java swing project (java 1.7) and tested successfully in eclipse
pushed the code to git for others to review and to be merged to master
once I got the go ahead after some minor changes (comments only), cloned it to a dev folder and keep it separate
ran mvn archetype:generate and create a mvn project w/ pom.xml
import this project to eclipse and retrofit the clean code from dev folder
modified pom.xml for dependency on the 3rd party jars (ess_es_server, ess_japi) and saved in eclipse.
came back to cmd prompt;compiled, packaged to create the jar- it was successful
while launching the app (java -cp < cp with jar) pkg.app, it gives error
“…java.lang.NoClassDefFoundError: com/essbase/api/base/EssException” whenever it's trying to invoke that particular class pertaining to one of the 3rd party jars
I have installed these 3rd party jars to my .m2 repo since they are not available in public repository.
Can someone help? My suspicion is my GAV parameters are not correct.
While running explicitly mention the library from where java program should pick up the jars; .m2 repo is used for compilation time only.

Categories

Resources