Failing Unit Test by GeneratedSerializationConstructorAccessor1
For running Gradle tasks, I prefer using a command-line interface. Recently, I made a code change and added new unit tests. To verify the behavior, I ran a test
task. Soon after, it showed a quite weird error message, which was about GeneratedSerializationConstructorAccessor1. As you can see below, the error message didn’t point out any line from my code. Everything was related to jdk
. When I made a pull request, TeamCity ran the test
task without any issue. It made me curious and I wanted to solve this issue.
The error message could vary slightly depending on your Jacoco tool version.
This error message showed some lines in jdk
. Since there was no issue in TeamCity, it might be a bug in jdk
and possibly I used a different jdk
version from what TeamCity used.
TeamCity used Java 8 and my machine used Java 12. I updated my Java version to work on backend services, which now requires higher that Java 11. After updating, Gradle seemed work well. I could run the assemble
task. The problem only happened for the test
task strangely.
After researching deeply, this turned out a quite complex problem. In short, Java 8 and higher versions (e.g. Java 9, Java 10, …) are different. Plugins or libraries should support higher versions to work properly. For me, they are Jacoco, Mockito, and PowerMock. There are quite typical plugins/libraries for Android testing.
For Jacoco, you can fix this issue by updating the plugin version and adding jacoco.excludes
.(Gradle issue #5184).
Mockito [had an update (Mockito PR #1250) to support this. If you see below error, you need to update mockito-core
to use the latest version. The current project used 2.8.9
version which is quite old and didn’t have that update. The latest Mockito version at this point is 2.28.2
and 3.0.0
.
For me, there was another hurdle. It still didn’t work and it was due to PowerMock. Unfortunately, it seemed not fixed yet (Powermock issue #901).
If you don’t use PowerMock library, updating libraries and setting excludes of Jacoco would fix your issue. In my case, however, it was tricky because of PowerMock. There might be some way to work around but it seemed not worth spending more time. As you know, the issue was caused by jdk
. It means that this could be easily solved by using Java 8 instead of Java 12. jenv
(https://www.jenv.be) helps to manage/switch multiple jdk
s in the same machine. Using that, I was able to fix all my issue and to switch Java 12 when I need to work on the backend code.
If you use Android Studio to run gradle tasks including
test
, you might not see this issue. Android Studio allows you to setjdk
. It could be different from your command line environment. By default, Android Studio uses an embedded JDK, which is Java 8.