0

我正在尝试一些代码。它是一种架构 Hibernate - JPA - Spring。现在,我希望在 JUnit 测试中运行它。

目前,我有一些例外:

GRAVE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6295eb] to prepare test instance [test.service.UserAccountServiceTest@609959]
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAccountService': Injection of autowired dependencies failed;
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private test.persistence.dao.UserAccountDao test.service.impl.UserAccountServiceImpl.userAccountDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [test.persistence.dao.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [test.persistence.dao.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.    
... 43 more
20 juil. 2012 10:56:19 test.service.UserAccountServiceTest tearDownOnce
INFO: tearDownOnce()

这里是 JUnit:UserServiceTest

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

import test.jndi.ContextDatasourceCreator;
import test.persistence.entity.UserAccount;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/spring/applicationContext.xml"})
public class UserAccountServiceTest extends Assert {

private static final Log LOG = LogFactory.getFactory().getInstance(UserAccountServiceTest.class);

@Autowired
@Qualifier("userAccountService")
private UserAccountService userAccountService;

@BeforeClass
public static void setUpOnce() {
    LOG.info("setUpOnce()");
    ContextDatasourceCreator.init();
}
@AfterClass
public static void tearDownOnce() {
    LOG.info("tearDownOnce()");
}

@Before
public void onSetUp() {
    LOG.info("onSetUp()");
}
@After
public void OnTearDown() {
    LOG.info("OnTearDown()");
}

@Test
public void testListAll() {
    List<UserAccount> allUserAccounts = userAccountService.getAllAccounts();
    for (UserAccount userAccount : allUserAccounts) {
        LOG.info(userAccount);
    }
}

}

/这里是我的应用上下文/

<!-- Annotations Scan -->
<context:annotation-config/>
<context:component-scan base-package="test.service, test.persistence" />

<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="dbrefPU" />
</bean>

<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- Transaction Annotations -->
<tx:annotation-driven proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />

/这里是我的源代码/

GenericDao 接口:

import java.util.List;

public interface GenericDao<T extends Object> {

T save(T pojo);
void remove(Class<T> classe, int id);
void delete(T pojo);
T findById(Class<T> classe, int id);    
List<T> findAll(Class<T> classe);
List<T> findByQuery(String jpql);
}

道接口:

import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}

GenericDao 实现:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import test.persistence.dao.GenericDao;

public abstract class GenericDaoImpl<T extends Object> implements GenericDao<T> {

@PersistenceContext
protected EntityManager em;

public T save(T pojo) {
    return em.merge(pojo);
}

public void remove(Class<T> classe, int id) {
    T pojo = findById(classe, id);
    if (pojo != null) {
        em.remove(pojo);
    }
}

public void delete(T pojo) {
    em.remove(pojo);
}

public T findById(Class<T> classe, int id) {
    return (T) em.find(classe, id);
}

public List<T> findAll(Class<T> classe) {
    StringBuffer jpql = new StringBuffer(20);
    jpql.append("from ").append(classe.getName());
    List<T> result = em.createQuery(jpql.toString()).getResultList();
    return result;
}

public List<T> findByQuery(String jpql) {
    List<T> result = em.createQuery(jpql).getResultList();
    return result;
}

}

道实现:

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import test.persistence.dao.UserAccountDao;
import test.persistence.entity.UserAccount;

@Repository("userAccountDao")
public class UserAccountDaoImpl extends GenericDaoImpl<UserAccount> implements     UserAccountDao {

public UserAccount findAccount(String matricule) {

    Query query = em.createNamedQuery("UserAccount.login");
    query.setParameter("matricule", matricule);

    UserAccount account = null;
    try {
        account = (UserAccount) query.getSingleResult();
    } catch (NoResultException nre) {

    }
    return account;
}

}

服务接口:

import java.util.List;

import test.persistence.entity.UserAccount;

public interface UserAccountService {

public abstract UserAccount login(String matricule);

public abstract UserAccount register(String matricule);

public abstract UserAccount getAccountWithId(Integer id);

public abstract List<UserAccount> getAllAccounts();

}

服务实现:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import test.persistence.dao.UserAccountDao;
import test.persistence.entity.UserAccount;
import test.service.UserAccountService;


@Service("userAccountService") 
@Transactional
public class UserAccountServiceImpl implements UserAccountService {

@Autowired
@Qualifier("userAccountDao")
private UserAccountDao userAccountDao;

public UserAccount getAccountWithId(Integer id) {
    return userAccountDao.findById(UserAccount.class, id);
}

public UserAccount login(String matricule) {
    return userAccountDao.findAccount(matricule);
}

public UserAccount register(String matricule) {
    UserAccount account = new UserAccount();
    account.setMatricule(matricule);

    try {
        account = userAccountDao.save(account);
    } catch (Exception e) {
    }
    return account;
}

public List<UserAccount> getAllAccounts() {
    return userAccountDao.findAll(UserAccount.class);
}

}

任何想法 ?非常感谢 !!

4

1 回答 1

0

我没有找到解决方案。在 Maven 中,它不起作用。

在动态 Web 项目中,如果我将 @PersistenceContext 更改为 EXTENDED,我将成功执行我的测试。

于 2012-08-08T12:46:15.480 回答