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

NoSuchBeanDefinitionException for @SpringBean and a bean implementing multiple interfaces #1006

Open
jpt-avaleo opened this issue Jun 28, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@jpt-avaleo
Copy link

commented Jun 28, 2019

So we have a bean that looks like this:

@Component
public class BeanImplementation implements InterfaceA, InterfaceB {

	@Override
	public String valueA() {
		return "Implementation value from interface A";
	}

	@Override
	public String valueB() {
		return "Implementation value from interface B";
	}
}

public interface InterfaceA {

	String valueA();

}

public interface InterfaceB {

	String valueB();
}

With test like this

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
class BeanImplementationTest extends Specification {

	@SpringBean
	InterfaceA interfaceA = Mock()

	@Autowired
	InterfaceB interfaceB

	def "should mock only interface A"() {
		given:
			interfaceA.valueA() >> "MOCK"
		expect:
			interfaceB.valueB() != null
	}

}

I get

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dk.kmd.nexus.hclsearch.integration.stock.BeanImplementationTest': Unsatisfied dependency expressed through field 'interfaceB'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'dk.kmd.nexus.hclsearch.integration.stock.InterfaceB' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.spockframework.runtime.model.FieldMetadata(initializer=false, line=15, name="interfaceB", ordinal=1)} - @ConditionalOnClass did not find required class 'javax.cache.CacheManager' (OnClassCondition)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) CachesEndpointAutoConfiguration:
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) Did not match:
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) - @ConditionalOnEnabledEndpoint no property management.endpoint.caches.enabled found so using user defined default from management.endpoints.enabled-by-default (OnEnabledEndpointCondition)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:391)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.spockframework.spring.SpringTestContextManager.prepareTestInstance(SpringTestContextManager.java:56)
at org.spockframework.spring.SpringInterceptor.interceptInitializerMethod(SpringInterceptor.java:43)
at org.spockframework.runtime.extension.AbstractMethodInterceptor.intercept(AbstractMethodInterceptor.java:24)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:97)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:475)
at org.spockframework.runtime.BaseSpecRunner.runInitializer(BaseSpecRunner.java:341)
at org.spockframework.runtime.BaseSpecRunner.runInitializer(BaseSpecRunner.java:336)
at org.spockframework.runtime.BaseSpecRunner.initializeAndRunIteration(BaseSpecRunner.java:274)
at org.spockframework.runtime.BaseSpecRunner.runSimpleFeature(BaseSpecRunner.java:266)
at org.spockframework.runtime.BaseSpecRunner.doRunFeature(BaseSpecRunner.java:260)
at org.spockframework.runtime.BaseSpecRunner$5.invoke(BaseSpecRunner.java:243)
at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:484)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:467)
at org.spockframework.runtime.BaseSpecRunner.runFeature(BaseSpecRunner.java:235)
at org.spockframework.runtime.BaseSpecRunner.runFeatures(BaseSpecRunner.java:185)
at org.spockframework.runtime.BaseSpecRunner.doRunSpec(BaseSpecRunner.java:95)
at org.spockframework.runtime.BaseSpecRunner$1.invoke(BaseSpecRunner.java:81)
at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:484)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:467)
at org.spockframework.runtime.BaseSpecRunner.runSpec(BaseSpecRunner.java:73)
at org.spockframework.runtime.BaseSpecRunner.run(BaseSpecRunner.java:64)

What I would expect to happen is to have InterfaceA mocked but InterfaceB referring to a real implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.