Announcement Announcement Module
Collapse
No announcement yet.
Autowire-Problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire-Problem

    Hey!

    Following situation:

    There's a service interface UserService

    Code:
    public interface UserService
    {
    ...
    	List<User> findAllUsers();
    ...
    }
    ..and the appropriate implementation UserServiceImpl

    Code:
    @Service
    public class UserServiceImpl implements UserService
    {
    	@Autowired
    	UserRepository userRepository;
    
    	public List<User> findAllUsers() {
    		return userRepository.findAll();
    	}
    ...
    }
    Now I try to write a JUnit test for my service implementation, it looks like this so far...

    Code:
    public class UserServiceImplTest
    {
    	@Autowired
    	public UserService userService;
    	
    ...
    }
    But when I run the test, I get the following error:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'net.twentyfourseven.blog.service.UserServiceImplTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public net.twentyfourseven.blog.service.UserService net.twentyfourseven.blog.service.UserServiceImplTest.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.twentyfourseven.blog.service.UserService] 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)}
    The implentation und interface are within the same package, and I scan my path via

    Code:
    <context:component-scan base-package="net.twentyfourseven.blog" />
    <context:annotation-config />
    Does anyones knows whats the problem here????

    Thank you!

    Markus

  • #2
    Judging from your code your testcase isn't loading anything... Also why would the context be creating your testcase?!

    Comment


    • #3
      Alternate approach to autowire

      Try to use the following to autowire,

      Code:
      	
      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
      instead of
      Code:
      <context:annotation-config />

      Comment


      • #4
        That is what annotation-config already does and which isn't needed because it is implied by the component-scan...

        Comment

        Working...
        X