Unwanted JUnit 4 Dependency with Kotlin and JUnit 5

I ran across this issue only by accident because I was investigating a completely different problem. I wrote a quick test to debug my issue and was wondering why custom serializers and deserializers are not registered with the Jackson ObjectMapper. I had a nice init() function that was annotated with @Before. So, what the hell?

Let’s back up a bit for some context.

  • Kotlin Project
  • Runs on Java 12
  • JUnit 5 as test engine
  • AssertK for assertions (just for the sake of completeness)

I’m used to JUnit 4, so in my test I used @Before to annotate a setup method. It was one of the many options IntelliJ presented to me.

@Before
fun init() {
    val module = SimpleModule()
    module.addDeserializer(Instant::class.java, InstantDeserializer())
    module.addSerializer(Instant::class.java, InstantSerializer())
    mapper.registerModule(module)
}

The method wasn’t called, however. But it’s annotated! Well, it’s the wrong annotation if you’re using JUnit 5. The correct one is @BeforeEach. This one and @BeforeClass (new name @BeforeAll) have been changed from version 4 to 5 to make their meaning more obvious.

But that’s besides the point. The question is: where does this @Before come from then?

A look at the dependency tree quickly reveals the culprit.

It’s the official JetBrains Kotlin JUnit test artifact. Although it doesn’t hurt me to have it in my project, it certainly caused some confusion and I’d like to avoid that in the future. Hence, I excluded the old version of JUnit in my POM file for this dependency.

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-test-junit</artifactId>
    <version>${kotlin.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Problem solved.

AdoptOpenJDK 8 NullPointerException sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)

I recently had to deal with this little bugger as we moved from the OpenJDK 8 package supplied by the Linux distro of choice to AdoptOpenJDK 8. It is important to know that we completely uninstalled OpenJDK, including all its transient dependencies.

(And in due time we’ll uninstall Java 8 and replace that grandpa as well)

As a result, parts of our application didn’t work any longer, resulting in this nice and shiny Java stacktrace.

2019-05-03 08:22:07,345 ERROR [qtp1896708863-35] [PlotChartController] [/][/][/]- error while creating chart image
java.lang.NullPointerException
        at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
        at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
        at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
        at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
        at sun.font.SunFontManager$2.run(SunFontManager.java:431)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.font.SunFontManager.<init>(SunFontManager.java:376)
        at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
        at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
        at java.awt.Font.getFont2D(Font.java:491)
        at java.awt.Font.defaultLineMetrics(Font.java:2176)
        at java.awt.Font.getLineMetrics(Font.java:2246)
        at org.jfree.chart.axis.DateAxis.estimateMaximumTickLabelWidth(DateAxis.java:1453)
        at org.jfree.chart.axis.DateAxis.selectHorizontalAutoTickUnit(DateAxis.java:1365)
        at org.jfree.chart.axis.DateAxis.selectAutoTickUnit(DateAxis.java:1340)
        at org.jfree.chart.axis.DateAxis.refreshTicksHorizontal(DateAxis.java:1616)
        at org.jfree.chart.axis.DateAxis.refreshTicks(DateAxis.java:1556)
        at org.jfree.chart.axis.ValueAxis.reserveSpace(ValueAxis.java:807)
        at org.jfree.chart.plot.CombinedDomainXYPlot.calculateAxisSpace(CombinedDomainXYPlot.java:364)
        at org.jfree.chart.plot.CombinedDomainXYPlot.draw(CombinedDomainXYPlot.java:442)
        at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1235)
        at org.jfree.chart.JFreeChart.createBufferedImage(JFreeChart.java:1409)
        at org.jfree.chart.JFreeChart.createBufferedImage(JFreeChart.java:1389)
        at org.jfree.chart.ChartUtilities.writeChartAsPNG(ChartUtilities.java:183)

I obviously removed some (a lot) parts to make it more readable and to hide corporate IP 😉 But this is the relevant part.

I found this bug report on Github and for once, plowing through the comments, it helped me. As is mentioned there, the culprit is the missing “fontconfig” package. So, I added another Ansible task to our playbook to provision the server et voila, the problem is gone.

- name: Install fontconfig package
  package:
    name: fontconfig
    state: present
    tags:
      - java

As mentioned earlier, we had wiped all that was relevant to OpenJDK off the system and by doing so, also uninstalled the “fontconfig” package. Otherwise this error wouldn’t have surfaced. But that’s the benefit of starting with a clean slate. This way you know if something is missing and don’t get surprised by errors all of a sudden while, at the same time, it is working on another machine.

Micrometer and Spring (Non-Boot)

Almost all of the tutorials and blog posts I found on this topic were focused on Spring Boot because, starting with version 2, it uses Micrometer as its metrics framework. However, in a particular project at work we do not have access to Spring Boot let alone a recent Spring version. Therefore, I’m explaining how to include Micrometer in your non-Boot Spring application using XML configuration.

In this tutorial I will be using Spring 5 and Java 11, so not exactly the versions I’m dealing with at work, but the concepts are the same and everything can probably be copied exactly as shown here.

Read More »

A Java “DSL“ for Simple Unit Test Data Creation

I’m a person that usually writes tests before the implementation. In the context of my backup application project this has turned out to really slow me down. But it’s not just a problem of my personal projects. It also affects my professional work. 

Here’s the issue: for some tests you need test data and generating that test data can be a tedious task, depending on the complexity. This has caused me to procrastinate on my backup app. So, one evening, after having thought about this during a workout, I grabbed my laptop, sat down in my comfy bed and wrote a “DSL” that makes creating the data much simpler. Not only is it easier to create the data now, allowing me to continue at a faster pace, it’s also much more readable and the test setup doesn’t clutter the test case anymore. This is a very important aspect of a test. What good does it to have one if, after some time, you have to update it and don’t understand what it does anymore?

Read More »

Using TagLib with pytaglib in Python

I tend to write a lot of background to paint a picture why I’m doing things, so I’ll try to keep it short for to move on to the code quickly.

I have a digital music collection that was sorted by the first letter of the artist (A, B, C etc.) and then the artist and underneath that the albums. While that is good to find things, it’s not optimal for listening in my car (via USB stick). Sometimes I find myself wanting to listen to all of Melodic Death Metal on shuffle play. My car doesn’t support this like iTunes, with its internal music library, which is why I wanted to group artists and albums by genre. Since I didn’t plan to do this all manually, I opted to write some scripts in Python.

The code is available on GitHub. If you have suggestions for improvements, please comment or create a pull request. I’m not a Python pro, so I’m sure there’s some room to make it better.

Read More »

Writing a Custom Backup Solution

If you are a user of any form of computer and care one bit about your sanity, then you probably have a backup strategy. Otherwise, if all hell breaks loose and your whole computer burns to ash or the hard drive melts to a heap of metal, turning it into an ugly door stop, you’ll likely be kinda angry, maybe slightly pissed, your pulse most definitely at 180, that you’ve lost all your data. I’d certainly be, especially about all my pictures of all the festivals and places I’ve been to. 

(And maybe some family 😅)

But, to be honest, I’ve been a bit lazy about backups for some time now. I do have copies of all my important files, but that’s not a backup. It’s a copy. A backup lets you go back in time and get an older version of a file or folder, not just the most recent one that has been synced.

So why is it, that I’m not as diligent as I should be? There are a few factors in that equation. It’s laziness for one, knowledge that I do have at least one copy, the fact that I haven’t had any data loss so far and stinginess. Why the latter? Up until now, being a Windows user (not any more though, on my main machine), I was relying on Acronis True Image, a commercial backup software. However, the version that I own – 2014, I think – stopped being reliable in one of the past Windows 10 versions. I simply don’t want to spend the money any more.

I’m not here to tell you that I have changed my mind on that. No. I’m, of course, coding my own solution. Why wouldn’t I? Everything is done multiple times in the Open Source community.

Read More »

Jules White Programming Cloud Services YouTube Video Series

In my search for information about what a web.xml exactly is and does, I ran across a video series on YouTube of Dr. Jules White who created over 70 videos explaining the basics and advanced topics of creating web services for mobile applications. The videos are roughly between 5 and 15 minutes long, so they are ideal for in-between watching, without sacrificing in content. You can binge them too, of course.

What I found most pleasing is that his presentation style is very informative and professional. There are no awkward pauses or anything else that would make me cringe. It’s very pleasant to watch and there’s a lot of good information in it, even for someone that already has a background in building web applications.

I created this list of links to all the individual videos because wanted to have more structure and information than a YouTube Playlist can provide in case I want to go back and watch something particular. Additionally, there’s a little sorting and numbering bug in the YouTube Playlist 😉

So, here you (or I) go.

Read More »