mockk: Bug: IllegalStateException: Could not self-attach to current VM using external process on Open JDK 11.0.1

We have main code in Java and tests in Kotlin. We build it and run the tests inside Docker container. All works locally but it does not work in our build environment. We have a Jenkins master running on AWS. It calls a slave node (also on AWS) to run the build. The build fails because of this library with the following error:

[WARNING] Corrupted STDOUT by directly writing to native stream in forked JVM 1. See FAQ web page and the dump file /home/mbuilder/workspace/....dumpstream
java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalStateException: Could not self-attach to current VM using external process
java.lang.NoClassDefFoundError: Could not initialize class io.mockk.impl.JvmMockKGateway

The dumpstream file is attached 2019-02-12T15-01-39_092-jvmRun1.dumpstream(had to add .log extention for GitHub). Also, note that OurClassTest mentioned in the dumpstream is only 283 lines long, so there is no line 290 that is referenced there.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 32

Most upvoted comments

I’ve received this error:

java.lang.NoClassDefFoundError: Could not initialize class io.mockk.impl.JvmMockKGateway

after upgrading to AS Arctic Fox using mockk 1.10.2

@oleksiyp so the code could just contain a single test with mockk<Serializable>() and the docker image is the following:

FROM amazonlinux:2

ARG MAVEN_VERSION=3.6.0
ARG BASE_URL=http://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries

RUN yum update -y  \
    && yum install -y python3 \
    && yum install -y tar \
    && yum install -y gzip

RUN pip3 install --upgrade pip \
    && pip3 install --upgrade --ignore-installed six awscli

RUN set -ex \
    && yum update -y

# TODO download this file (https://jdk.java.net/archive/) and place it inside the image here /openjdk-11.0.1_linux-x64_bin.tar.gz
RUN tar zxf /openjdk-11.0.1_linux-x64_bin.tar.gz -C /usr/local \
    && rm /openjdk-11.0.1_linux-x64_bin.tar.gz

RUN alternatives --install /usr/bin/java java /usr/local/jdk-11.0.1/bin/java 1 \
    && alternatives --install /usr/bin/javac javac /usr/local/jdk-11.0.1/bin/javac 1 \
    && yum clean all

ENV JAVA_HOME=/usr/local/jdk-11.0.1

RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

ENV MAVEN_HOME /usr/share/maven

RUN mkdir -p /maven-repo

Then we run it like this (in the Jenkisfile):

script {
    docker.build("image-name:${env.BUILD_ID}")
    env.HOST_M2 = "******/.m2"
}
sh """
docker run \
 --network="host" \
 --user root:root \
 -v ${pwd()}:/app \
 --workdir /app \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v ${env.HOST_M2}:/root/.m2 \
 image-name:${env.BUILD_ID} \
 /bin/bash -c "mvn clean test"
"""

MockK uses a bit different way(inlining) then Mockito uses by default(subclassing) to overcome final classes problem.

Try switching mockito to inlining mode https://hadihariri.com/2016/10/04/Mocking-Kotlin-With-Mockito/#mockmaker. It probably will have same thing.

Anyway I am reopening ticket. Just want let you know that I check such things during weekends(and even not during each weekend). So please be patient