I have several test codes for APIs that are important than others.
They do the test with MockMvc and are placed in 'test' package, seperated from the original codes.
Is there any way to run all of the test codes when production server (the @SpringBootApplication) starts up??

- 187
- 1
- 1
- 11
-
If you're trying to get the unit test package to be executed by the startup of the SpringBoot Jar, then... that's very unusual, and the test code would never be compiled into it. However, it looks like you've written an integration test pack that you want to somehow include in the deployment pipeline. In that instance, you'd expect your continuous deployment process to separately execute your test pack from the same source tree. That said, integration tests probably ought to be in their own project, rather than mixed with production code. – Ashley Frieze Dec 01 '21 at 14:19
-
@AshleyFrieze Do you mean that test code has to be in completely different project, not just a test folder separated from src folder ? – Flippingflop Dec 01 '21 at 14:31
-
Usually tests are done during build time only what are the tests doing that you need to run them on application startup? Take a look into spring tests, you can create a web context in tests. And you can test them in build time only.https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html – shockwave Dec 01 '21 at 18:39
1 Answers
You typically place your test code in a separate test
folder (parallel to the src
) folder.
You don't want to run the test code on every production startup, because if a server fails, it should restart quickly.
You don't even want to run test code in production in general. You will eventually have tests that disable security mechanisms just to test that these mechanisms work. Other tests will need to access internal code, and you'll have to disable parts of the security to give your test code access.
You want nothing of that to be even present in production, so what you do is do the testing in the deployment pipeline, running the test code in the build phase.
The challenge is that your test environment will have to be as identical as possible to production, but without accessing actual production data.

- 754
- 7
- 22