我想解决以下问题。
我创建了一个Spring Boot应用程序,它也使用了Spring Data。 要进行集成测试,我想启动H2数据库。
我使用@ContextConfiguration
配置了测试并引用了我的applicationContext.xml文件。
当我从IDE(intelliJ)运行测试时,一切都很好,测试正在变得很好。
但是只要我在构建服务器上或在带有gradle的控制台中运行测试,我就会得到NoSuchBeanDefinitionException
。似乎根本没有考虑applicationContext ......
其实我不知道该怎么做..
我的applicationContext-test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:data="http://www.springframework.org/schema/data/jpa"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="de.company.project"/>
<bean class=
"org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class=
"org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence-
test.xml"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect"/>
</bean>
</property>
</bean>
</beans>
我的persistenc-test.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<class>de.company.project.server.model.Entity</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.ejb.naming_strategy"
value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.default_schema" value="public" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
database.properties只包含H2数据库的默认值。
最后但并非最不重要的是我的Testclass
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:*META-INF/applicationContext-test.xml")
public class EntityServiceTest {
@Test
public void doSomeStuff(){
}
.
.
}
我希望有人知道我做错了什么?!
BR
编辑:
错过了添加例外和存储库:
@Repository
public interface EntityRepository extends JpaRepository<Entity, Integer> {
}
例外:
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying
bean of type [de.company.project.server.dao.EntityRepository] 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)}
答案 0 :(得分:1)
我通过将TransactionalTestExecutionListener.class
添加到我的TestExecutionListeners来修复它。
答案 1 :(得分:0)
我认为@Repository
注释应该在实现而不是接口上。我假设你在其他地方有这个接口@Autowired
。 Spring需要知道应该使用哪个具体类。
此外,在具有单元测试的类路径中可能找不到配置文件。将测试上下文配置放在src/test/resources
中,并将配置注释更改为:
@ContextConfiguration(locations = "classpath:/applicationContext-test.xml")
有关详细信息,请参阅此答案(有关同一问题的其他问题):https://stackoverflow.com/a/4377840/3851006