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

Trump Asks Supreme Court To Let Him Fire Lisa Cook

Trump Asks Supreme Court To Let Him Fire Lisa Cook

The post Trump Asks Supreme Court To Let Him Fire Lisa Cook appeared on BitcoinEthereumNews.com. Topline President Donald Trump asked the Supreme Court on Thursday to let him fire Federal Reserve Governor Lisa Cook, calling on justices to pause a lower court ruling that blocked the termination, as the president continues his efforts to fire Cook despite reports showing she did not have the improper mortgage applications the Trump administration claimed. Federal Reserve Board of Governors member Lisa Cook at an open meeting of the Board of Governors at the Federal Reserve on June 25, 2025, in Washington. Copyright 2025 The Associated Press. All rights reserved. Key Facts Trump asked the Supreme Court to pause an appeals court ruling that found he did not have the authority to fire Cook, after the president said he was firing the Fed governor over alleged issues with her mortgage filings. Federal law only allows Trump to fire members of the Federal Reserve’s board “for cause,” which typically refers to some kind of gross misconduct, and two federal courts have now ruled that Cook could only be fired for some kind of issue with her job performance at the Fed, and not for any personal issues with her mortgage. Trump claims he did fire Cook “for cause,” however, pointing to the board member’s role overseeing monetary policy and arguing to the Supreme Court that “strong concerns about the appearance of mortgage fraud … satisfies any conception of cause.” Cook has strongly denied any wrongdoing, and recent reports indicate she did not claim two properties as primary residences as the Trump administration had claimed, with Reuters reporting Cook correctly labeled her home in Atlanta as being a “vacation home” on a loan application, and officials in Michigan confirming there was no impropriety regarding her other residence in Ann Arbor. The case has the potential to broadly clear the way for…
Share
2025/09/19 03:53
3 Paradoxes of Altcoin Season in September

3 Paradoxes of Altcoin Season in September

The post 3 Paradoxes of Altcoin Season in September appeared on BitcoinEthereumNews.com. Analyses and data indicate that the crypto market is experiencing its most active altcoin season since early 2025, with many altcoins outperforming Bitcoin. However, behind this excitement lies a paradox. Most retail investors remain uneasy as their portfolios show little to no profit. This article outlines the main reasons behind this situation. Altcoin Market Cap Rises but Dominance Shrinks Sponsored TradingView data shows that the TOTAL3 market cap (excluding BTC and ETH) reached a new high of over $1.1 trillion in September. Yet the share of OTHERS (excluding the top 10) has declined since 2022, now standing at just 8%. OTHERS Dominance And TOTAL3 Capitalization. Source: TradingView. In past cycles, such as 2017 and 2021, TOTAL3 and OTHERS.D rose together. That trend reflected capital flowing not only into large-cap altcoins but also into mid-cap and low-cap ones. The current divergence shows that capital is concentrated in stablecoins and a handful of top-10 altcoins such as SOL, XRP, BNB, DOG, HYPE, and LINK. Smaller altcoins receive far less liquidity, making it hard for their prices to return to levels where investors previously bought. This creates a situation where only a few win while most face losses. Retail investors also tend to diversify across many coins instead of adding size to top altcoins. That explains why many portfolios remain stagnant despite a broader market rally. Sponsored “Position sizing is everything. Many people hold 25–30 tokens at once. A 100x on a token that makes up only 1% of your portfolio won’t meaningfully change your life. It’s better to make a few high-conviction bets than to overdiversify,” analyst The DeFi Investor said. Altcoin Index Surges but Investor Sentiment Remains Cautious The Altcoin Season Index from Blockchain Center now stands at 80 points. This indicates that over 80% of the top 50 altcoins outperformed…
Share
2025/09/18 01:43