NoSuchBeanDefinitionException:PlatformTransactionManager在Spring Boot 2.0.0.M6中运行Junit测试

时间:2017-11-28 08:54:01

标签: spring spring-mvc spring-boot junit

我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件生成了Spring Boot Web应用程序。 当我在正常模式下运行应用程序时,它似乎工作正常

使用的技术:

Spring Boot 2.0.0.M6,Java 8,maven和我没有使用JPA,而是使用jdbcTemplate

我有这个JUnit测试:

@SpringBootTest
@ContextConfiguration(classes={PersistenceConfig.class})
@RunWith(SpringRunner.class)
public class JdbcMetricsRepositoryTests {

    @Autowired
    MetricsRepository jdbcMetricsRepository;

    @Test
    public void testGetAllDeviceEventsWithAlarm() throws DataAccessException, SQLException {

        //jdbcMetricsRepository.getIberiaDataTab1();

        assertNotNull (jdbcMetricsRepository.getIberiaDataTab1());
    }
}

但是当我运行测试时,我收到了这个错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:394)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:284)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy37.getIberiaDataTab1(Unknown Source)
    at com.iberia.repository.JdbcMetricsRepositoryTests.testGetAllDeviceEventsWithAlarm(JdbcMetricsRepositoryTests.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这里是persistenceConfig

@Configuration
@Profile("prod")
@EnableTransactionManagement /* Defines a Bean Post-Processor (proxies @Transactional bean) */
public class PersistenceConfig {

    @Value("${db.driver.class.name}")
    private String dbDriverClassName;

    @Value("${db.jdbc.url}")
    private String dbJdbcUrl;

    @Value("${db.user}")
    private String dbUser ;

    @Value("${db.pwd}")
    private String dbPwd;


    @Value("${db.pool.size}")
    private Integer dbPoolSize;

    @Value("${db.minimum.idle}")
    private Integer dbMinimumIdle;

    @Bean
    public  JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }


    @Bean 
    public UserRepository userRepository() {
        return new JdbcUserRepository();
    }


    @Bean 
    public RemoteUnitEventRepository remoteUnitEventRepository() {
        return new JdbcRemoteUnitEventRepository();
    }


    @Bean 
    public MetricsRepository metricsRepository() {
        return new JdbcMetricsRepository();
    }




    /**
     * Creates an in-memory "ideefe" database populated 
     * with test data for fast testing
     */
    @Bean
    public DataSource dataSource(){

        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(getDbPoolSize());
        config.setMinimumIdle(5);
        config.setDriverClassName(getDbDriverClassName());
        config.setJdbcUrl(getDbJdbcUrl());
        config.addDataSourceProperty("user", getDbUser());
        config.addDataSourceProperty("password", getDbPwd());

        return new HikariDataSource(config);
    }

    public String getDbDriverClassName() {
        return dbDriverClassName;
    }


    public String getDbJdbcUrl() {
        return dbJdbcUrl;
    }


    public String getDbUser() {
        return dbUser;
    }


    public String getDbPwd() {
        return dbPwd;
    }


    public int getDbPoolSize() {
        return dbPoolSize;
    }


    public int getDbMinimumIdle() {
        return dbMinimumIdle;
    } 
}

这是我的pom.xml

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
         <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
         <dependency> 
            <groupId>oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.2</version>
        </dependency>
         <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
         <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <!-- <version>4.5.3</version> -->
        </dependency>

    </dependencies>

0 个答案:

没有答案