Ah, now I understand it perfectly. It is a crucial detail and a very common pattern in unit tests with Mockito: when populatePisRecords is a static method in a utility class, we cannot spy on the service with @Spy, but rather we must mock the call to the static method itself.
To achieve this, a different Mockito feature is used, available through the mockito-inline dependency. This dependency allows temporarily mocking static methods during the execution of a specific test.
Dependency verification (essential): ensure that mockito-inline is included in the project's pom.xml or build.gradle. The standard spring-boot-starter-test package may not include it by default. If errors appear when trying to mock static methods, explicitly add the dependency with groupId org.mockito and artifactId mockito-inline in test scope.
Updated test and recommended approach: use Mockito.mockStatic within a try-with-resources block. That is the modern and safe way to mock static methods; it guarantees that the mock is only active within the block and is automatically cleaned up upon exit.
Key steps in the test: 1) create the test objects and the expected ProducerRecord; 2) open a try-with-resources block with MockedStatic of the utility class; 3) use mockedIntakeUtils.when with lambda syntax to stub IntakeUtils.populatePisRecords and return the desired map; 4) execute the method under test inside the try block so that the mock is active; 5) verify interactions with kafkaTemplate.
Use of try-with-resources: it is the recommended syntax because it prevents the static mock from leaking into other tests and causing unexpected and difficult-to-debug failures.
Lambda syntax when stubbing static methods: you should use mockedIntakeUtils.when(() -> IntakeUtils.populatePisRecords(...)).thenReturn(...) or the equivalent form with matchers. It is important that the act and assert are executed within the try block so that the mock is in effect.
Suggested test cases: send a successful record and verify that kafkaTemplate.send was invoked; handle the case where the ProducerRecord has a null topic and verify that send is not called; simulate that kafkaTemplate throws an exception and verify that the service catches it and does not propagate it; return an empty map and verify that no message is sent.
Good practices and brief explanation: 1 Do not use @Spy on the class under test when only external dependencies need to be replaced. 2 Use @InjectMocks to inject mocks into the class being tested. 3 Use Mockito.mockStatic to control static methods of a utility class. 4 Ensure that the scope of the mock is the minimum necessary using try-with-resources.
If the description is empty, an article is created from the title Hzha and useful information is included about the implementation and unit tests with Mockito for static methods.
About Q2BSTUDIO: Q2BSTUDIO is a custom software development and custom applications company specialized in artificial intelligence, cybersecurity, and cloud services. We offer custom software for companies that need personalized solutions, implementation of AI agents, and AI projects for companies that optimize processes and generate value. We provide AWS and Azure cloud services, integration of solutions with Power BI, and business intelligence services to transform data into actionable insights. Our cybersecurity expertise ensures that every custom application and every artificial intelligence project is deployed with robust security controls and good practices.
Featured services of Q2BSTUDIO: custom application development and custom software, implementation of artificial intelligence and AI agents, AI solutions for companies, managed cybersecurity, migration and deployment on AWS and Azure cloud services, and business intelligence platforms including Power BI. We are creative and results-oriented, accompanying from conception to production and ongoing support.
Keywords for positioning: custom applications, custom software, artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, AI for companies, AI agents, Power BI. By integrating these technologies, we help companies automate tasks, improve decision-making, and protect their digital assets.
If you need help adapting unit tests, adding mockito-inline, or designing a custom solution with artificial intelligence and security, the Q2BSTUDIO team can advise you and execute the entire project from analysis to delivery.


