Most Tests and examples include an xml config file. I have a project setup mostly with annotations and base spring 3 config. I can't get the tests to run since I get an error that no matching bean of type is found:
Caused by:
Code:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [xxx.service.ProfileService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 28 more
applicationContext-core.xml
persistence.xml
Code:
<persistence-unit name="profile" transaction-type="RESOURCE_LOCAL" >
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>xxx.entity.BMSProfile</class>
<properties>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
</properties>
</persistence-unit>
</persistence>
ProfileServiceImpl.java
Code:
@Service("profileService")
public class ProfileServiceImpl implements ProfileService {
@PersistenceContext
EntityManager em;
final EmailService emailService;
// Not used unless you declare a <protect-pointcut>
@Pointcut("execution(* xxx.ProfileServiceImpl.*(..))")
public void myPointcut() {
}
@Inject
public ProfileServiceImpl(final EmailService emailService) {
this.emailService = emailService;
}
public List<BMSProfile> findAccounts() {
return em.createNamedQuery("findAllAccounts").getResultList();
}
public List<BMSProfile> findAccountById(Long abnId) {
return em.createNamedQuery("findAccountById").setParameter("abnId", abnId).getResultList();
}
public BMSProfile post(BMSProfile account) {
return null;
}
public BMSProfile readAccount(Long id) {
return em.find(BMSProfile.class, id);
}
}
Testclass
Code:
@RunWith
(SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = {
"classpath*:applicationContext-core.xml",
"classpath*:applicationContext-mail.xml",
"classpath*:applicationContext-security.xml" })
@TransactionConfiguration
@Transactional
public class ProfileServiceTest {
@Inject
private ProfileService profileService;
@Test
publicvoid testSave() {
List<BMSProfile> list = profileService.findAccounts();
assertEquals(list.size(), 1);
}
}
As you can see, it's a straight forward Spring app using JSR330 injections and <context:annotation-config /> and <context:component-scan/> is setup in spring-core as well. It doesn't make a difference if @Autowired is used instead of @Inject.
Also, the config files are in the classpath while junit tests are running.
I got this working in 2.5x in my earlier projects. What am I missing here?