You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@fineract.apache.org by Adam Monsen <am...@mifos.org> on 2025/08/21 22:32:10 UTC

local integration test failures - MockitoException - static mocking is already registered in the current thread

Hi all, I'd love some help making my tests run locally. I've got a
confusing and consistent failure.

I'm trying to recreate more or less what appears in
.github/workflows/build-postgresql.yml , but without the matrix/sharding.
I'm standing up a postgres db and mock oauth2 server, then running the
following:

gradle createPGDB -PdbName=fineract_tenants

gradle createPGDB -PdbName=fineract_default

gradle --no-continue test -PdbType=postgresql -x checkstyleJmh -x
checkstyleMain -x checkstyleTest -x spotlessCheck -x spotlessApply -x
spotbugsMain -x spotbugsTest -x javadoc -x javadocJar -x modernizer -x
:twofactor-tests:test -x :oauth2-test:test -x
:fineract-e2e-tests-runner:test

I've been using something close to this for months now without issues, but
since about a week ago I've started getting a MockitoException. I ran it
just now and got: 420 tests, 1 failures, 0 ignored, 47.916s duration.
Failed tests: RepaymentPeriodTest. initializationError

full results: https://adammonsen.com/tmp/2025-08-21-001-gradle-test/

Any ideas?

Re: local integration test failures - MockitoException - static mocking is already registered in the current thread

Posted by Adam Monsen <am...@mifos.org>.
Thanks Paul! Yes, that does help. I see Ádám created this PR
<https://github.com/apache/fineract/pull/4975>. I tried that fix locally
and it worked! I also tried your suggestion here
<https://github.com/apache/fineract/commits/auto-close-static-mocks> and
that also works (ignore the test aggregation gradle code in the 1st two WIP
commits, that's just a patch I use to see all test results in one
report) (compare
my branch with develop
<https://github.com/apache/fineract/compare/develop...auto-close-static-mocks>).
I'm not sure which approach is better. I'm naive but I think I prefer
try-with-resources
<https://github.com/apache/fineract/pull/4975#issuecomment-3215103263>.

Re: local integration test failures - MockitoException - static mocking is already registered in the current thread

Posted by Paul <pc...@gmail.com>.
See if this is helpful:
MoneyHelper is being created, but a previous test or a setup method is not
properly "de-registered" or closed.

The most reliable and recommended way to de-register a static mock is by
using a *try-with-resources* block. This Java feature ensures that the mock
is automatically closed and deregistered at the end of the block,
preventing resource leaks and conflicts.
@Test void myTestThatMocksAStaticMethod() { try
(MockedStatic<MyStaticClass> mockedStatic =
Mockito.mockStatic(MyStaticClass.class))
{ // Here, you define the behavior of the static method
mockedStatic.when(MyStaticClass::someStaticMethod).thenReturn("mocked value"
);
  // Your test logic that calls the static method String result =
MyStaticClass.someStaticMethod();
  // Assertions assertEquals("mocked value", result); }
 // The mock is automatically closed and deregistered here }

On Thu, Aug 21, 2025 at 5:33 PM Adam Monsen <am...@mifos.org> wrote:

> Hi all, I'd love some help making my tests run locally. I've got a
> confusing and consistent failure.
>
> I'm trying to recreate more or less what appears in
> .github/workflows/build-postgresql.yml , but without the matrix/sharding.
> I'm standing up a postgres db and mock oauth2 server, then running the
> following:
>
> gradle createPGDB -PdbName=fineract_tenants
>
> gradle createPGDB -PdbName=fineract_default
>
> gradle --no-continue test -PdbType=postgresql -x checkstyleJmh -x
> checkstyleMain -x checkstyleTest -x spotlessCheck -x spotlessApply -x
> spotbugsMain -x spotbugsTest -x javadoc -x javadocJar -x modernizer -x
> :twofactor-tests:test -x :oauth2-test:test -x
> :fineract-e2e-tests-runner:test
>
> I've been using something close to this for months now without issues, but
> since about a week ago I've started getting a MockitoException. I ran it
> just now and got: 420 tests, 1 failures, 0 ignored, 47.916s duration.
> Failed tests: RepaymentPeriodTest. initializationError
>
> full results: https://adammonsen.com/tmp/2025-08-21-001-gradle-test/
>
> Any ideas?
>


-- 
--
Paul