0

The structure of my SpringBoot project is:

enter image description here

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?

msrd0
  • 7,816
  • 9
  • 47
  • 82
powerseed
  • 1,090
  • 3
  • 17
  • 29
  • 1
    Does this answer your question? [How to start up spring-boot application via command line?](https://stackoverflow.com/questions/47835901/how-to-start-up-spring-boot-application-via-command-line) – D.B. Jul 25 '20 at 00:55
  • @D.B. So the command should be `java -jar CNSClient.jar fully.qualified.package.Send1000EmailsIn24hours` assuming I am running the console in the same directory as the jar file? But this command returns `no main manifest attribute, in CNSClient.jar`. – powerseed Jul 25 '20 at 01:04
  • Why would you want to do that? Is that some sort of integration testing... – z atef Jul 25 '20 at 04:22
  • Normally test code does not go to your production code jar. So you need to make additional steps - depending on which build tool you are using (Maven, Gradle or something else) - there was a similar question already raised here https://stackoverflow.com/questions/36047637/how-can-i-include-test-classes-into-maven-jar-and-execute-them). It is worth to look I believe. Anyway, I'm still struggling to understand why do you need it this way. When you can use build tool itself Maven or Gradle to run your test from command line themselves... – Alexei Kovalev Jul 25 '20 at 05:00

1 Answers1

1

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
davidxxx
  • 125,838
  • 23
  • 214
  • 215