【发布时间】:2015-09-29 15:03:19
【问题描述】:
我正在努力理解为什么会发生此错误。我正在将教程移植到最新版本的 Spring、Hibernate 和 WildFly。我从命令行运行,使用 Maven 构建和测试应用程序。我收到以下错误:
2015 年 7 月 10 日下午 2:18:03 org.springframework.test.context.TestContextManager prepareTestInstance 严重:在允许 TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@523884b2] 准备测试实例 [com.russ.home.test.dao.CompanyDaoTest@131774fe] 时捕获异常 org.springframework.beans.factory.BeanCreationException:创建名为“com.russ.home.test.dao.CompanyDaoTest”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource);嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [javax.sql.DataSource] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释:{}
这是我的测试应用程序上下文文件:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties" />
<bean id="tttDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
/>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true"
p:databasePlatform="org.hibernate.dialect.MySQLDialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="ttt-jpa"
p:dataSource-ref="tttDataSource"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:persistenceXmlLocation="classpath*:META-INF/test-persistence.xml"
/>
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:dataSource-ref="tttDataSource"
p:entityManagerFactory-ref="entityManagerFactory"/>
<!-- checks for annotated configured beans -->
<context:annotation-config/>
这是抽象测试类
@WebAppConfiguration
@ContextConfiguration
({
"classpath*: /testingContext.xml",
})
public abstract class AbstractDaoForTesting extends AbstractTransactionalJUnit4SpringContextTests {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired(required = true)
protected CompanyDao companyDao;
@Autowired(required = true)
protected ProjectDao projectDao;
@Autowired(required = true)
protected TaskDao taskDao;
@Autowired(required = true)
protected UserDao userDao;
@Autowired(required = true)
protected TaskLogDao taskLogDao;
}
这是 CompanyDAOTest 类。
@RunWith(SpringJUnit4ClassRunner.class)
public class CompanyDaoTest extends AbstractDaoForTesting {
public CompanyDaoTest(){}
/**
* Test case for the find(id) method of the CompanyDao implementation
* @throws Exception
*/
@Test
public void testFind() throws Exception {
logger.debug("\nSTARTED testFind()\n");
List<Company> allItems = companyDao.findAll();
assertTrue(allItems.size() > 0);
// get the first item in the list
Company c1 = allItems.get(0);
int id = c1.getId();
Company c2 = companyDao.find(id);
assertTrue(c1.equals(c2));
logger.debug("\nFINISHED testFind()\n");
}
任何建议将不胜感激。
罗斯
【问题讨论】:
-
我能看到 com.russ.home.test.dao.CompanyDaoTest 吗?
标签: java spring hibernate maven spring-mvc