This is a pretty straightforward way to test untestable code.This is a pretty straightforward way to test untestable code.

Testing the Untestable: A Simple Way to Handle Static Methods in Legacy Java

2025/10/24 12:22

I'm currently working on a software designed more than a decade ago. It offers a plugin architecture: you can develop a plugin whose lifecycle is handled by the software. The tough part, though, is how you access the platform capabilities: via static methods on singletons.

@Override public boolean start() { var aService = AService.getInstance(); var anotherService = AnotherService.getInstance(); // Do something with the services var result = ...; return result; }

There's no easy way to test the start() method. In the old days, Mockito developers had pushed back against this feature, and the only alternative was PowerMock. The decision was reversed in 2020 with the 3.4.0 release, which introduced static method mocking in Mockito.

I liked the previous situation better. My opinion is that having to mock static methods is a sign of badly designed code. I wrote about it already ten years ago. With PowerMock, one could mock the static methods, write the test, redesign the code, and then remove PowerMock. With the current situation, one can't look at the dependencies to search for design smells. In any case, the above problem still stands, and I can't change the design. It's forced upon me.

The solution is strangely straightforward, though. Just write a wrapper method around the one:

@VisibleForTesting //1 boolean start(AService aService, AnotherService anotherService) { //2 // Do something with the services var result = ...; return result; } @Override public boolean start() { var aService = AService.getInstance(); var anotherService = AnotherService.getInstance(); return start(aService, anotherService); //3 }

  1. Method is normally private, but since we want to test it, we make it package visible. The @VisibleForTesting annotation is for documentation purposes.
  2. The testable method has parameters that can be mocked
  3. Call the testable method

In this post, I showed how one can test legacy code not built on Dependency Injection. This is a pretty straightforward way to test untestable code.


Originally published at A Java Geek on October 19th, 2025

Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.
Share Insights

You May Also Like

Crypto.com Applies for U.S. Bank Charter, Joining Coinbase, Ripple and Circle

Crypto.com Applies for U.S. Bank Charter, Joining Coinbase, Ripple and Circle

Crypto.com filed an application with the U.S. Office of the Comptroller of the Currency (OCC) to become a National Trust Bank in a major push to expand its custody and staking services for corporate and institutional investors.Digital assets meet tradfi in London at the fmls25Expanding Custody and StakingThe charter would reportedly allow the crypto exchange to offer custody and staking across multiple blockchains and digital asset protocols, including Cronos. The company aims to attract Digital Asset Treasuries, Exchange Traded Funds, and other institutional clients, providing a federally regulated alternative for managing digital assets.“Building the Crypto.com product and service portfolio through regulated and secure offerings has been our focus since day one,” said Kris Marszalek, Co-Founder and CEO of Crypto.com.“We are excited to take this next step by filing for a National Trust Bank Charter and look forward to continuing to pursue opportunities to provide customers with the trusted services they require.”The filing will not affect Crypto.com Custody Trust Company’s ongoing operations. The company remains a Qualified Custodian regulated by the New Hampshire Banking Department.A Step Toward Institutional LeadershipBy pursuing federal regulation, Crypto.com aims to position itself as a custody partner for corporate investors while maintaining flexibility to innovate in digital asset management. Crypto.com joins other crypto exchanges seeking a banking charter. Recently, Ripple applied for the same to allow the crypto firm to expand its services nationwide under federal oversight.True to our long-standing compliance roots, @Ripple is applying for a national bank charter from the OCC. If approved, we would have both state (via NYDFS) and federal oversight, a new (and unique!) benchmark for trust in the stablecoin market.Earlier in the week via… https://t.co/IdiR7x3eWZ— Brad Garlinghouse (@bgarlinghouse) July 2, 2025Around the same time, Stablecoin issuer Circle also submitted an application to the U.S. Office of the Comptroller of the Currency to establish a national trust bank to oversee its stablecoin reserve. “Establishing a national digital currency trust bank of this kind marks a significant milestone in our goal to build an internet financial system that is transparent, efficient, and accessible,” said Circle Co-Founder, Chairman, and CEO Jeremy Allaire. Coinbase also joined the banking charter bandwagon this month as crypto eyes mainstream adoption. This article was written by Jared Kirui at www.financemagnates.com.
Share
2025/10/24 22:08