1

Right now I have my Dockerfile as below:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

What if I want to run some junit test after I start the springboot application? I am using Maven for dependencies.

Where should I put those lines?

mvn test
mvn clean test
mvn clean compile test

Or what other commands should I use?

tripleee
  • 175,061
  • 34
  • 275
  • 318
lllllll
  • 319
  • 1
  • 4
  • 13
  • 1
    To run unit tests you should write unit tests which will be executed during. the build of your application before the application has been packaged into a jar file. This is usually done via `mvn clean test`. Put your unit tests into `src/test/java` and name them like `*Test.java`. If you have spring boot tests tests you should name them like `*IT.java` and will be run during the integration test phase which can be executed via `mvn clean verify`. Furthermore you should read about the build life cycle https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html – khmarbaise Mar 25 '20 at 06:13
  • Yes I have written my junit test in the correct directory. But I would like to know how to execute it in docker file – lllllll Mar 25 '20 at 06:37
  • If you want to execute the test build, why are you starting the actual app? The tests can't depend on the app running, or the app wouldn't build, or am I missing something? – daniu Mar 25 '20 at 06:49
  • In the docker file either you only package your final application or you build inside a docker container...the question is: What would you like to do? – khmarbaise Mar 25 '20 at 06:55
  • I am trying to push springboot application to travis ci, and run junit test on travis ci. so I need to define the command in docker file – lllllll Mar 25 '20 at 07:36

1 Answers1

3

Premise: Even if the solution is oriented to your specifics it would be better to execute tests during target jar build phase

To execute tests on your Dockerfile you can do one of the following:

  1. copy also source files on Dockerfile and execute maven test commands. Doing this you can also build your target jar directly on the Container and also you need maven to be installed on the Container.

  2. copy just the target jar file and use it to execute your tests you need to include test-classes on the target jar. ( See at How can I include test classes into Maven jar and execute them? ).

Regardless the way you choose, you can modify your entrypoint to execute multiple commands. You can do it basically in 2 ways:

  1. (a) Creating a bash script that executes your commands( See at Multiple commands on docker ENTRYPOINT )
  2. (b) Using supervisord ( See at How to write a Dockerfile which I can start a service and run a shell and also accept arguments for the shell? ). This is a better solution in order to manage process related to the container life ( so the process with PID 1 ).

Example

Let's suppose you choose to copy all source files ( option 1 ) and use a bash script ( option 1(a) ) to do it. Create the command.sh file as follow in order to attach container to Spring application process even if mvn test will be executed:

#!/bin/bash

#Execute Spring application
CMD="java -jar target/app.jar"
$CMD &
SERVICE_PID=$!

#Execute Tests
mvn test

#Wait for Spring execution
wait "$SERVICE_PID"

Your Dockerfile will look like follow:

#Start from maven docker image
FROM maven:3.6.1-jdk-8-alpine

#Copy all sources
COPY . .

#Build ( because you want to execute tests after the spring boot application is started you should disable test during build phase )
RUN mvn install

#Start container
COPY commands.sh /scripts/commands.sh
RUN ["chmod", "+x", "/scripts/commands.sh"]
ENTRYPOINT ["/scripts/commands.sh"]
gregorycallea
  • 1,218
  • 1
  • 9
  • 28
  • /scripts/commands.sh: line 11: wait: `': not a pid or valid job spec ? – lllllll Apr 07 '20 at 19:09
  • This happens when you pass an empty argument to **wait** command. My issue on writing **command.sh** script missing the execution command `$CMD &`. I'm going to update the answer. Please retry. – gregorycallea Apr 08 '20 at 09:33
  • Hi I still confused about the process. I looking for the following achievement in the my docker file: mvn install, mvn test, and build the jar file, and I am also using this docker file to deploy it on aws elasticbean stalk as my applications backend server. How should my docker file look like? Thanks – lllllll Apr 14 '20 at 05:30