Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute JUnit tests inside Docker container

Tags:

java

docker

junit

I would like to build a test environment with Docker, where I can remotely send JUnit test classes (including the code that is tested), execute the tests and retrieve the results.

I found some articles which explained how to use docker for testing databaseconntection/writing inside a redis, but not how i can simple let my tests perform on docker and retrieve the results.

Do you have any recommendations how You would actually achieve this? I don't know much about Jenkins, but would this might solve my problem? Is there any good framework outside for this?

like image 300
duichwer Avatar asked Aug 14 '26 22:08

duichwer


1 Answers

One way I found that works (using Gradle) is as follows. I know you are specifically referencing JUnit as your testing framework, but I actually think something similar to this could work.

Dockerfile (I called mine Dockerfile.UnitTests):

FROM gradle:jdk8 AS test-stage
WORKDIR /app
COPY . ./
RUN gradle clean
RUN gradle test

FROM scratch AS export-stage
COPY --from=test-stage /app/build/reports/tests/test/* /

I then run this with (in Gitbash on Windows 10):

> DOCKER_BUILDKIT=1 docker build -f Dockerfile.UnitTests --output type=tar,dest=UnitTests.tar .

This results in a tar file containing the test results displayed in an html file.

I executed the above in a Gitlab CI/CD pipeline and then sent the results to a web API for analysis.

A couple of assumptions:

  1. My project is set up for Gradle builds so I have the structure from the root of my project src/test/java/groupname/projectname/testfile.java
  2. I am working in Windows 10 targeting Linux containers and using Gitbash.
like image 153
joshmcode Avatar answered Aug 16 '26 11:08

joshmcode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!