简体   繁体   中英

sharing properties between spring and non-spring tests

Most of my DB tests don't use Spring because this way they start much faster. But I want to also run smoke tests that actually run entire Spring Boot application.

And I would like to have DB connection parameters in one place. To do it I think I have 2 options:

  1. simulate / reuse Spring's properties loading in my non-Spring tests
  2. inject my properties calculated in runtime (annotation with a constant is not an option) into Spring configuration in smoke tests

Any idea how to achieve any of those? Or maybe there is some simpler way?

You can export your database connection parameters as system properties named exactly as properties from your application.properties . Thanks to the way Spring Boot handles externalized configuration, those values will override those from the properties (or YAML, whichever you use) file in Spring configuration, and you then just need to use the system properties in your non-Spring tests.

Although I would suggest making those DB tests a part of Spring suite, too - you actually gain speed only when you execute a single test; unless you have @DirtiesContext tests or tests that use different @ActiveProfiles annotation, Spring will cache the application context and only initialize it once per whole test suite.

Here's a quote from the official documentation :

The Spring TestContext Framework provides consistent loading of Spring ApplicationContexts and WebApplicationContexts as well as caching of those contexts. Support for the caching of loaded contexts is important, because startup time can become an issue — not because of the overhead of Spring itself, but because the objects instantiated by the Spring container take time to instantiate. For example, a project with 50 to 100 Hibernate mapping files might take 10 to 20 seconds to load the mapping files, and incurring that cost before running every test in every test fixture leads to slower overall test runs that reduce developer productivity.

By default, once loaded, the configured ApplicationContext is reused for each test. Thus the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term test suite means all tests run in the same JVM — for example, all tests run from an Ant, Maven, or Gradle build for a given project or module.

You can store your DB connection properties in a dedicated Java db.properties file that you can load for your standard tests and Spring Boot tests.

You can then instruct Spring Boot to load db.properties in addition to your standard Spring Boot application properties files by specifying a custom spring.config.location .

The Application property files section of the Spring Boot reference manual provides all the details you should need.

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