【问题标题】:Running integration tests with Spring boot (NoSuchBeanDefinitionException)使用 Spring Boot 运行集成测试 (NoSuchBeanDefinitionException)
【发布时间】:2015-02-10 06:09:19
【问题描述】:

我正在尝试解决以下问题。

我创建了一个同样使用 Spring Data 的 Spring Boot 应用程序。 为了进行集成测试,我想启动一个 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 数据库的默认值。

最后但并非最不重要的是我的测试类

@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)}

【问题讨论】:

    标签: java intellij-idea gradle integration-testing spring-boot


    【解决方案1】:

    我通过将 TransactionalTestExecutionListener.class 添加到我的 TestExecutionListeners 来修复它。

    【讨论】:

      【解决方案2】:

      我认为@Repository 注释应该在实现而不是接口上。我假设你在其他地方有这个接口@Autowired。 Spring 需要知道应该使用哪个具体类。

      此外,在带有单元测试的类路径中可能找不到配置文件。将测试上下文配置放在src/test/resources 中,并将配置注释更改为:

      @ContextConfiguration(locations = "classpath:/applicationContext-test.xml")

      有关更多信息,请参阅此答案(以及其他相同问题的答案):https://stackoverflow.com/a/4377840/3851006

      【讨论】:

      • 你也可以在 Spring-Data 文档中看到,它只是使用接口并自动创建实现。
      • 好的,添加了类路径问题。
      • 不,还是同样的问题。我认为这是我缺少弹簧靴的东西......
      猜你喜欢
      • 1970-01-01
      • 2020-02-10
      • 2018-02-01
      • 2020-07-24
      • 2015-10-03
      • 1970-01-01
      • 2015-01-01
      • 2020-02-25
      • 1970-01-01
      相关资源
      最近更新 更多