Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamBridge Final Cannot Be Mocked

I am not able to mock StreamBridge in my Unit Tests.

@MockBean
private StreamBridge bridge;

I get an IllegalStateException due to:

Caused by: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class org.springframework.cloud.stream.function.StreamBridge Mockito cannot mock/spy because :

  • final class at org.springframework.boot.test.mock.mockito.MockDefinition.createMock(MockDefinition.java:154)
like image 654
Rubens Gomes Avatar asked Aug 01 '26 05:08

Rubens Gomes


1 Answers

StreamBridge is a final class, With Mockito2 we can mock the final class, but by default this feature is disabled and that need to enable with below steps:

  1. Create a directory ‘mockito-extensions’ in src/test/resources/ folder.

  2. Create a file ‘org.mockito.plugins.MockMaker’ in ‘src/test/resources/mockito-extensions/’ directory.

  3. Write the content 'mock-maker-inline' in org.mockito.plugins.MockMaker file.

  4. At test class level use ‘@ExtendWith(MockitoExtension.class)’

Then StreamBridge will be mocked successfully.

like image 80
Manish Kumar Avatar answered Aug 03 '26 17:08

Manish Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!