Search code examples
javaswinggitlabheadless

Testing Swing elements in a Headless Environment (gitlab CI)


I have a gradle project using gitlab's CI, unforunately using any Swing classes results in:
java.awt.AWTException: headless environment

I found one answer at Easiest way to unit test SWT and Swing apps in a headless environment? that mentioned using Xvfb, which seems like it would work, but didn't elaborate because there was a tool-specific simpler option. If Xvfb would work, how does it need to be configured in my project? I couldn't find any resources for gitlab/gradle. Is there a simpler option specific to gitlab?


Solution

  • So I was able to find a solution that has just worked. I ended up creating my own Docker image:

    FROM alpine:3.10
    RUN apk update
    RUN apk --no-cache add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
    RUN apk add xvfb-run
    

    and then I just had to update my .gitlab-ci.yml to use that image:

    image: jeffreydm/xvfb-java:v0.1
    

    and lastly, I updated my script from:

    build:
      stage: build
      script:
        - ./gradlew build
    

    to:

    build:
      stage: build
      script:
        - xvfb-run ./gradlew build