Running JUnit tests together with Java project - java

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

Related

IntelliJ fails on every test on a maven imported project

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?

Using maven to create junit xml reports of jar

I have inherited a codebase whereby we have a maven project of component tests that use junit as the framework that runs the tests.
Everything runs OK, but the problem is that these tests must be compiled into a jar-with-dependencies so that they can be run on a standalone computer with very limited outwards connectivity (e.g does not have access to maven central).
I have managed to create a runnable jar with dependencies which I can run using junit.jar and the command line like so:
java -cp jar-with-dependencies.jar:junit.jar junit.jar org.junit.runner.JUnitCore com.testsuite.AllTests
This works but the problem is that I also need to output the junit results into XML, like maven's surefire plugin does.
My question is, can I run this jar-with-dependencies using maven such that it creates the junit xml reports?
I can successfully run the tests using exec-maven-plugin which essentially runs the previously stated command

How to run selenium test cases in jenkins?

I had been introduced to concept of CI lately and was trying to work on jenkins CI. I was stuck up in one thing . How to trigger executable testng files in jenkins CI. For ex locally in our machines we just run testng.xml to execute couple of test cases. In the same way how can we trigger this xml file to run in jenkins CI ?
In most cases with jenkins you wouldn't use an executable. Normally you'd run the wrapper for the tests (Junit/Nunit etc.) which Jenkins is fully capable of running on it's own.
You can use this article to run TestNG tests using Maven:
Running TestNG tests using maven
After configuration is completed just add Invoke top-level Maven targets step to the Build Steps in Jenkins (Maven plugin should be installed). The target should be test in this case.
If you will face with any errors during configuration, try to google them.
If you are not using any build tool like maven or ant, you can invoke it from command line as we'll and specify your suite file. Make sure to set the correct class paths http://testng.org/doc/documentation-main.html#running-testng
You can put this as a build step in Jenkins.
Add a compilation step prior to this step. I haven't ever tried it - have always used ant or maven, but that is where I would start exploring.

How to run Maven unit test (testng) on Eclipse?

I have a Maven project and I have included some unit tests. I can run those unit tests from command line using
mvn test -Dtest=AppTest
It will run the unit test (AppTest class) without any problems. But if I tried to run the test on Eclipse as JUnitTest, I got an error saying
"No Tests found with test runner 'JUnit 3'"
I know the test (AppTest) is not a JUnit test but I didn't see "maven" option if I right clicked on the test class.
Do you know how I could run the tests on Eclipse?
If those are testng tests, then you can download the eclipse plugin for TestNG and right click and run them as test class. Testng plugin here
If you want to run the command line way of maven, you would need the maven plugin for eclipse from here
Then you can right click your project and you should see an option of Run As -> Maven Test. You would need to set relevant arguments in the surefire plugin of pom or use run configuration to specify parameters like you did in -Dtest
I would suggest the testng plugin which would be simpler to just select the class and run the class or a single case or a package even.

How can I run the IntelliJ debugger on unit tests in a Maven project?

I'm working with a multi-artifact Maven project where artifacts in the project have a few dependencies on each other. I am using IntelliJ 9. I'd like to be able to set breakpoints in my unit tests, but when I right-click on the unit tests folder for my artifact and choose "Debug 'All Tests'", I get a Class not found exception referring to a class in a separate Maven artifact.
I can run the Maven "test" goal on the parent artifact and it works fine.
Any ideas? Thanks.
In you run Maven from command line, you will be able to run it with debugger enabled and just attach Idea as remote debugger. That's how I usually use it.
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" test
See http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html
This will allow debugger connection to port 8000 and wait for you to attach before execution.
I wanted to run the unit tests for a specific package.
I was able to get this to work by making a new JUnit run/debug configuration in IntelliJ. I told it to run the tests in the specific package and for "Use classpath and JDK of module", I picked the root Maven artifact.

Categories

Resources