Issue
The structure of my SpringBoot project is:
There is no main class, and I have compiled the project into a jar
file.
Now I want to run a method named "testEmailSending" in the test class shown in the very last line of the screenshot, Send1000EmailsIn24hours
, from the jar
file from command line.
The reason why I want to run this test in a jar
file is that the whole project is just a test, but I think that since it is a test, so I shouldn't put the code in the main
class, instead, putting it in a test would be better.
I am not sure if this is correct. Should I put the code for testing just in the main class?
Solution
As Alexei Kovalev implies, you never want to do that.
Test code is test code. Good practice is to never include it in the application and maven/gradle honor that by default.
With spring boot, you are advised to execute tests and integration test in the frame of the tools provided by the framework : Annotating your test with@SpringBootTest
achieves that goal by loading a spring context as you will do by running your jar.
If besides these kinds of test you need to perform tests on a JAR, you may of course do that. In a some measure, it may make sense since a JAR may have some packaging differences that could be tested. But you should write few tests for that. Because packaging a full spring boot application is a slow process and also because you could not benefit from all test facilities provided by Spring Boot.
The overall idea for that :
- write integration tests (JUnit is enough as runner) that are not packaged in the application and that calls the component (via web requests for api/controllers) or check these side effects (for batch processings).
- run the application as a jar
- run integration tests
Answered By - davidxxx