Running tests


JHipster comes with an extensive set of tests, and each generated application has:

  • Integration tests using the Spring Test Context framework.
  • UI tests with Karma.js.
  • Performance tests with Gatling.

We have two goals in generating those tests:

  • Help every JHipster user to follow best practices, as we believe tests are a very useful part of every application
  • Validate that what is being generated is correct. So even if you don't plan to use those tests at all, doing just a mvn clean test and grunt test after generating your application is a good way of knowing if everything is fine. You are then free to ignore those tests if you think that testing is a waste of time!

All those tests will be generated in the standard Maven src/test folder.

Integration tests

Integration tests are done with the Spring Test Context framework, and are located in the src/test/java folder. JHipster will launch a specific Spring test context, which will be re-used along all tests, as:

  • Your Spring beans should be stateless and thread-safe, and thus can be re-used across your different tests suites.
  • Launching just one Spring context for all tests if a lot faster than launching a new Spring context for each test.

This Spring test context will use a specific test database to execute its tests:

  • If you use an SQL database, JHipster will launch an in-memory H2 instance in order to use a temporary database for its integration tests. Liquibase will be run automatically, and will generate the database schema.
  • If you use Cassandra, JHipster will launch an in-memory Cassandra instance usinng CassandraUnit.
  • If you use MongoDB, as this is not a Java-based database, you will need to run a specific MongoDB instance on your local machine.
  • If you use Elasticsearch, JHipster will launch an in-memory Elasticsearch instance using Spring Data Elasticsearch.

Those tests can be run directly in your IDE, by right-clicking on each test class, or by running mvn clean test (or ./gradlew test if you run Gradle).

Limitations: if the generated entities have validation enabled, JHipster is not enable to generate the correct values depending on the validation rules. Those rules can be so complex, for example if a Regex pattern is used, that this just not possible. In this case, the tests will fail validation, and the default values used in the test will need to changed manually, so they can pass the validation rules.

UI tests

UI tests are done with Karma.js and PhantomJS, and are located in the src/test/javascript folder.

Those tests will mock up the access to the application's REST endpoints, so you can test your UI layer without having to launch the Java back-end.

Those tests can be run using grunt test (or gulp test if you use Gulp.js).

Performance tests

Performance tests are done with Gatling, and are located in the src/test/gatling folder. They are generated for each entity, and allows to test each of them with a lot of concurrent user requests.

Gatling tests can be run with Maven, by running mvn gatling:execute. If you have several tests, JHipster will ask which test should be run.

Warning! At the moment, those tests do not take into account the validation rules you may have enforced on your entities. You will anyway need to change those tests, according to your business rules, so here are few tips to improve your tests:

  • On your running application, go to the Administration > Logs screen, and put org.springframework in debug mode. You will see the validation errors, for example.
  • Use the application normaly and open the Chrome console log: you will be able to see the REST requests with all their parameters, including the HTTP headers.