A photo of Geoffrey Hayward

Java JUnit: Reset a singleton

Published February 28, 2018

Illustration of Java code resetting a singleton in a JUnit.

Java singleton’s are hard to unit test because the state of the singleton is altered as each test runs. But for testing sake you can reset the singleton’s state use reflection. Here is an example that worked for me.

@Before
public void setup() throws NoSuchFieldException, IllegalAccessException {
        Field instance = SomeSingleton.class.getDeclaredField("set");
        instance.setAccessible(true);
        instance.setBoolean(null, false);
}

I hope this helps.

Latest Posts

A simple graph that says configure Terraform Cloud to assume an AWS IAM role via OIDC.

Configure Terraform Cloud to Assume an AWS IAM Role via OIDC

June 11, 2025

Computing

Here is how to configure Terraform Cloud to assume an AWS IAM role via OIDC (OpenID Connect) using only environment variables—no static AWS keys are required.

Continue reading
Developer with laptop and friendly robot assistant outdoors, coding with Spring Boot.

Responsible AI Coding: What I’ve Learned So Far

May 9, 2025

Computing

A whole new way of coding. When JetBrains offered me a trial of Junie, an AI assistant built into IntelliJ, I had to give it a go. I soon saw the potential, it’s kind of like an AI coding partner (albeit a little dumb at times), but that’s okay, if used responsibly. So here’s what I’ve learned about coding responsibly with AI—so far.

Continue reading
And abstract image of programming code surrounded by a light bulb.

Notes on Successfully Running Ubuntu in VirtualBox

January 26, 2024

Computing

As a Software Developer who likes to use Ubuntu for Development but can sometimes only run it as just another window in Windows, I would like to share some nuanced VirtualBox configurations I built up over the years.

Continue reading