Skip to content
New issue

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

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

Already on GitHub? # to your account

Determine beanName just like what @MockBean do #10

Closed
tan9 opened this issue Aug 31, 2017 · 4 comments
Closed

Determine beanName just like what @MockBean do #10

tan9 opened this issue Aug 31, 2017 · 4 comments

Comments

@tan9
Copy link

tan9 commented Aug 31, 2017

Spring Boot's @MockBean determines beanName automatically:

	private String determineBeanName(String[] existingBeans, SpyDefinition definition,
			BeanDefinitionRegistry registry) {
		if (StringUtils.hasText(definition.getName())) {
			return definition.getName();
		}
		if (existingBeans.length == 1) {
			return existingBeans[0];
		}
		return determinePrimaryCandidate(registry, existingBeans,
				definition.getTypeToSpy());
	}

So that the mock will automatically override existing beans without raising NoUniqueBeanDefinitionException like:

No qualifying bean of type 'com.cht.commons.security.login.UserRegistry' available: expected single matching bean but found 2: userRegistry,userRegistryMock

@pchudzik
Copy link
Owner

I will look into it, and figure something out to make it work more like in spring.

Right now bean name generation is based only on the field name.
Until it's fixed you can set mock name using name() attribute on @AutowiredMock and @AutowiredSpy
Or you can simply name your field userRegistry it will replace original userService with the mock.

pchudzik added a commit that referenced this issue Sep 13, 2017
@pchudzik
Copy link
Owner

pchudzik commented Sep 13, 2017

So something like this will do right?

Will be released in 1.1.1 (sooner than latter ;)).
It's released as snapshot version - 1.1.1-SNAPSHOT

Feel free to reopen if that's not what you've expected.

@tan9
Copy link
Author

tan9 commented Sep 19, 2017

@pchudzik I can confirm the issues has been fixed properly.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants