简体   繁体   中英

Running integration tests with Spring boot (NoSuchBeanDefinitionException)

I am trying to sort out the following issue.

I've created a Spring Boot application which is also using Spring Data. To do integration tests, I want to power up an H2 database.

I configured the test with @ContextConfiguration and referenced my applicationContext.xml file.

When I am running the tests out of the IDE (intelliJ) everything is fine, and the test is getting grean. But as soon as I run the test on my build server or in the console with gradle, I get NoSuchBeanDefinitionException . It seems like the applicationContext is not considered at all...

Actually I have no clue anymore what to do..

My 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>

My 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>

The database.properties just contains the defaults for an H2 database.

And last but not least my Testclass

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:*META-INF/applicationContext-test.xml")
public class EntityServiceTest {
  @Test
  public void doSomeStuff(){
  }
.
.
}

I hope somebody has a clue what I am doing wrong?!

BR

Edit:

Missed to add the exception and the respository:

 @Repository
 public interface EntityRepository extends JpaRepository<Entity, Integer> {
 }

Exception:

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

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

I think the @Repository annotation should be on the implementation and not the interface. I assume you have this interface @Autowired somewhere else. Spring needs to know which concrete class should be used.

Also, the config file may not be found on the classpath with unit tests. Place the test context config in src/test/resources and change the configuration annotation to:

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

See this answer (and others for the same question) for more info: https://stackoverflow.com/a/4377840/3851006

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM