Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bean override with REPLACE_OR_CREATE_DEFINITION and byType lookup fails if no match is found #32990

Closed
snicoll opened this issue Jun 8, 2024 · 0 comments
Assignees
Labels
in: test Issues in the test module type: bug A general bug
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented Jun 8, 2024

Consider the following scenario:

@SpringJunitConfig
public class MyTest {

  @MockitoBean
  private ExampleService service;

}

With zero ExampleService in the context. MockitoBean has a REPLACE_OR_CREATE_DEFINITION strategy, which means it should create a (mock) bean for that type if none is found.

Right now it fails as:

Caused by: java.lang.IllegalStateException: Unable to select a bean definition to override: found 0 bean definitions of type org.springframework.test.context.bean.override.example.ExampleService (as required by annotated field 'ZeroCandidatesTestCase.example')
	at org.springframework.test.context.bean.override.BeanOverrideBeanFactoryPostProcessor.registerReplaceDefinition(BeanOverrideBeanFactoryPostProcessor.java:143) ~[main/:?]
	at org.springframework.test.context.bean.override.BeanOverrideBeanFactoryPostProcessor.registerBeanOverride(BeanOverrideBeanFactoryPostProcessor.java:123) ~[main/:?]
	at org.springframework.test.context.bean.override.BeanOverrideBeanFactoryPostProcessor.postProcessWithRegistry(BeanOverrideBeanFactoryPostProcessor.java:99) ~[main/:?]
	at org.springframework.test.context.bean.override.BeanOverrideBeanFactoryPostProcessor.postProcessBeanFactory(BeanOverrideBeanFactoryPostProcessor.java:89) ~[main/:?]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) ~[main/:?]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:197) ~[main/:?]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:790) ~[main/:?]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[main/:?]
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:221) ~[main/:?]
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:110) ~[main/:?]
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:212) ~[main/:?]
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:225) ~[main/:?]
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:152) ~[main/:?]
	... 72 more

There's even an integration test to assert the (faulty) scenario.

@snicoll snicoll added in: test Issues in the test module type: bug A general bug labels Jun 8, 2024
@snicoll snicoll added this to the 6.2.0-M4 milestone Jun 8, 2024
@snicoll snicoll self-assigned this Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: bug A general bug
Projects
None yet
Development

No branches or pull requests

1 participant