NoSuchBeanDefinitionException:PlatformTransactionManager 在 Spring Boot 2.0.0.M6 中运行 Junit 测试
Posted
技术标签:
【中文标题】NoSuchBeanDefinitionException:PlatformTransactionManager 在 Spring Boot 2.0.0.M6 中运行 Junit 测试【英文标题】:NoSuchBeanDefinitionException: PlatformTransactionManager running Junit Tests in Spring Boot 2.0.0.M6 【发布时间】:2018-05-11 15:57:25 【问题描述】:我使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序,并将其打包为可执行 JAR 文件。 当我在正常模式下运行应用程序时,它似乎工作正常
使用的技术:
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>
【问题讨论】:
PersistenceConfig 看起来像...? 你需要有一个@Bean 方法来返回一个PlatformTransactionManager 的实例。以baeldung.com/transaction-configuration-with-jpa-and-spring为例 但我没有使用 JPA 所以你没有在你的测试中使用自动配置,并且仍然期望 spring boot 自动配置一切。你为什么这么努力不使用 Spring Boot? 【参考方案1】:由于您重新定义了数据源,您可以通过定义下面的 bean 来定义您的 PlatformTransactionManger
@Bean
public DataSourceTransactionManager transactionManager()
final DataSourceTransactionManager txManager = new DataSourceTransactionManager();
txManager.setDataSource(dataSource);
return txManager;
【讨论】:
但我没有使用 JPA 对于那些需要 DataSouce 但不介意使用 h2(嵌入式内存数据库)的人:JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setURL("jdbc:h2:mem:test"); txManager.setDataSource(dataSource);
并添加到您的 pom.xml:<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.199</version> <scope>test</scope> </dependency>
以上是关于NoSuchBeanDefinitionException:PlatformTransactionManager 在 Spring Boot 2.0.0.M6 中运行 Junit 测试的主要内容,如果未能解决你的问题,请参考以下文章