0
votes

I have following test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class CompoundServiceImplTest {

    @Autowired
    @Qualifier("testCompoundService")
    private TestCompoundService testCompoundService;

    //...
}

and ApplicationContext contains:

<bean id="testCompoundService"  autowire="byType"
      class="myPackage.TestCompoundService">
</bean>

If also tried autowire byName or leaving @Qualifier away (I added that because it did not work but that did not help either).

I get following exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No matching bean of type [myPackage.TestCompoundService] found for dependency: 
        expected at least 1 bean which qualifies as autowire candidate for this dependency. 
        Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true),
        @org.springframework.beans.factory.annotation.Qualifier(value=testCompoundService)}

The bean is clearly configure yet spring claims it isn't?

How can i solve this?

EDIT:

When I change @Autowired to @Resource I get following error:

Injection of resource dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
    Bean named 'testCompoundService' must be of type [myPackage.TestCompoundService], 
    but was actually of type [$Proxy68]
1

1 Answers

0
votes

The solution:

http://blog.nigelsim.org/2011/05/31/spring-autowired-use-interfaces/

TestCompoundService was a concrete class. Solution is to create an interface and use that in code and onyl in spring configuration use concrete class TestCompoundServiceImpl.