@MockBean not working for mybatis repositories after upgrading spring boot version from 2.1 to 2.3

57 Views Asked by At

@MockBean is not working for mybatis repositories after upgrading the spring boot version from 2.1 to 2.3. While running the UT, it calls the actual method instead of the mocked method, hence my unit test failed. Please help.

It works for other components like services and controllers.

Code from the Test class

@MockBean
private StockQuantityRepository stockQuantityRepository;
Mockito.doReturn(lockedList)
                    .when(stockQuantityRepository)
                    .lockStockQuantityList(Mockito.anyList(), Mockito.anyInt()); 
                    //here framework is injecting mokced bean of stockQuantityRepository {StockQuantityRepository$MockitoMock$500783717@11328}
            

Code from the service implementation class

lockedStockList = stockQuantityRepository.lockStockQuantityList(processList, lockWaitTimeout); 
 //here framework is injecting actual bean of stockQuantityRepository {$Proxy132@15694)

While debuging it is showing that {StockQuantityRepository$MockitoMock$500783717@11328} in Test java class and {$Proxy132@15694) in the service class, which means in the test class it is injecting mocked bean, but in the service class it is injecting actual bean.

0

There are 0 best solutions below