简体   繁体   中英

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

  1. Create a JUnit configuration for your tests

  2. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM