当我尝试构建项目时,我在测试阶段遇到异常。
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'com.dozortsev.adviceexchange.service.test.UserServiceTest':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: public com.dozortsev.adviceexchange.service.UserService
com.dozortsev.adviceexchange.service.test.TestContext.userService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.dozortsev.adviceexchange.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)}
项目包含多个模块:
AdviceExchange
├── domain
├── dao
├── service
├── web
我不明白为什么会发生这种情况,所有DAO类都映射为服务层上的@Repository
所有映射为@Service
的类。
看起来像我的 DAO 层的应用程序上下文:
<beans xmlns="http://www.springframework.org/schema/beans"....>
<import resource="classpath:/spring/queries.xml"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/adviceexchange"/>
<property name="username" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.dozortsev.adviceexchange.domain"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
测试的应用程序上下文:
<beans xmlns="http://www.springframework.org/schema/beans"...>
<import resource="classpath:/spring/dao-application-context.xml"/>
<context:annotation-config/>
<context:component-scan base-package="com.dozortsev.adviceexchange"/>
<!--
Define configuration for embedded database
Build HSQL database for unit testing in-memory
-->
<jdbc:embedded-database id="embeddedDatabase" type="HSQL">
<jdbc:script location="classpath:database/test-ddl.sql"/>
<jdbc:script location="classpath:database/test-dml.sql"/>
</jdbc:embedded-database>
</beans>
还有我的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:/spring/test-application-context.xml")
public abstract class TestContext {
static final Logger log = Logger.getLogger(TestContext.class);
@Autowired public UserService userService;
private static EmbeddedDatabase db;
@BeforeClass public static void setUp() {
log.info("creates an HSQL in-memory database");
db = new EmbeddedDatabaseBuilder().setName("embeddedDatabase").build();
}
@AfterClass public static void tearDown() {
log.info("shutdown database");
db.shutdown();
}
}
UserServiceTest 是在构建项目期间抛出异常的类
public class UserServiceTest extends TestContext {
@Test public void testFindByLogin() {
final String login = "sed.dui.Fusce@torquentperconubia.net";
Assert.assertNotNull(userService);
User user = userService.findByLogin(login);
Assert.assertNotNull(user);
}
@Test public void testCreate() {
// prepare data for service
User user = new User(
"Mario", 25, null, "Germany, Dortmund", null, "gotze@gmail.com", "gotze_mario", 1
);
Assert.assertNull(user.getId());
userService.create(user);
Assert.assertNotNull(user.getId());
}
}
请帮我解决这个问题。我很感谢所有的建议!
答案 0 :(得分:1)
此文件夹错误
https://github.com/dozortsev/AdviceExchange/tree/master/service/src/test/resource
maven约定是“资源” - 最后是s,因此测试应用程序上下文不在类路径中。
因为你有classpath *:/ spring / test-app-context ... with *忽略了丢失的文件然后它找不到任何bean,因为上下文是空的。
只需修复文件夹名称。