Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change springs behavior on NoSuchBeanDefinitionException

In integration tests with spring application context I write quite often something like this in my configuration classes

@Bean
public MyComponent myComponent() {
   return mock(MyComponent.class);
}

Sometimes I have to create those beans just to fulfill dependency requirements, sometimes I wire those mocks in my test to configure them.

In any case I'd love to get rid off those bean definitions. Is there a way to tell spring to create a mock of a type it is missing instead of throwing NoSuchBeanDefinitionException?

like image 739
mibutec Avatar asked Nov 28 '25 11:11

mibutec


1 Answers

Have a look at spring-auto-mock.

By registering the AutoMockRegistryPostProcessor all the dependencies that Spring cannot find will be automagically converted to mockito mocks.

Here you have a test example.

like image 88
Ruben Avatar answered Nov 30 '25 00:11

Ruben



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!