不满足的依赖性 运行 Spring 引导中的测试
Unsatisfied dependency running a Test in Spring Boot
我使用 Spring Initializr 生成了一个 Spring Boot web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring 启动 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6 , 行家 3, Java 8
我有这个测试:
@ContextConfiguration(classes={PersistenceConfig.class})
@RunWith(SpringRunner.class)
public class BooksManagerTests {
/**
* The object being tested.
*/
@Autowired
BooksManager booksManager;
@Test
public void testfindDeviceByKey() {
booksManager.findDeviceByKey("C380F");
}
}
@Configuration
public class PersistenceConfig {
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public BooksManager booksManager() {
return new BooksManagerImpl();
}
/**
* Creates an in-memory "rewards" database populated
* with test data for fast testing
*/
@Bean
public DataSource dataSource(){
return
(new EmbeddedDatabaseBuilder())
.addScript("classpath:db/H2.schema.sql")
.addScript("classpath:db/H2.data.sql")
.build();
}
}
@Service("booksManager")
public class BooksManagerImpl implements BooksManager {
private DeviceEventRepository deviceEventRepository;
@Autowired
public void setDeviceEventRepository(DeviceEventRepository deviceEventRepository) {
this.deviceEventRepository = deviceEventRepository;
}
@Override
public List<DeviceEvent> getAllDeviceEvents() {
return deviceEventRepository.getAllDeviceEvents();
}
}
@Repository("deviceEventRepository")
public class JdbcDeviceEventRepository implements DeviceEventRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<DeviceEvent> getAllDeviceEvents() {
String sql = "select * from t_device_event";
return mapDeviceEvents(jdbcTemplate.queryForList(sql));
}
private List<DeviceEvent> mapDeviceEvents(List<Map<String,Object>> deviceEventsMap) {
return null;
}
}
但是我得到这个错误 运行 测试:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'booksManager': Unsatisfied dependency expressed through method 'setDeviceEventRepository' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.repository.DeviceEventRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
您的应用程序在初始化期间仅指定了一个配置:
@ContextConfiguration(classes={PersistenceConfig.class})
您需要像这样指定一个 main 来启动您的应用程序以启用 Spring 启动自动配置:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
有关 JPA 存储库自动配置的详细信息,请参阅 JpaRepositoriesAutoConfiguration。
解决了将此添加到 PersistenceConfig class
@Bean
public DeviceEventRepository deviceEventRepository() {
return new JdbcDeviceEventRepository();
}
我使用 Spring Initializr 生成了一个 Spring Boot web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring 启动 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6 , 行家 3, Java 8
我有这个测试:
@ContextConfiguration(classes={PersistenceConfig.class})
@RunWith(SpringRunner.class)
public class BooksManagerTests {
/**
* The object being tested.
*/
@Autowired
BooksManager booksManager;
@Test
public void testfindDeviceByKey() {
booksManager.findDeviceByKey("C380F");
}
}
@Configuration
public class PersistenceConfig {
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public BooksManager booksManager() {
return new BooksManagerImpl();
}
/**
* Creates an in-memory "rewards" database populated
* with test data for fast testing
*/
@Bean
public DataSource dataSource(){
return
(new EmbeddedDatabaseBuilder())
.addScript("classpath:db/H2.schema.sql")
.addScript("classpath:db/H2.data.sql")
.build();
}
}
@Service("booksManager")
public class BooksManagerImpl implements BooksManager {
private DeviceEventRepository deviceEventRepository;
@Autowired
public void setDeviceEventRepository(DeviceEventRepository deviceEventRepository) {
this.deviceEventRepository = deviceEventRepository;
}
@Override
public List<DeviceEvent> getAllDeviceEvents() {
return deviceEventRepository.getAllDeviceEvents();
}
}
@Repository("deviceEventRepository")
public class JdbcDeviceEventRepository implements DeviceEventRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<DeviceEvent> getAllDeviceEvents() {
String sql = "select * from t_device_event";
return mapDeviceEvents(jdbcTemplate.queryForList(sql));
}
private List<DeviceEvent> mapDeviceEvents(List<Map<String,Object>> deviceEventsMap) {
return null;
}
}
但是我得到这个错误 运行 测试:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'booksManager': Unsatisfied dependency expressed through method 'setDeviceEventRepository' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.repository.DeviceEventRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
您的应用程序在初始化期间仅指定了一个配置:
@ContextConfiguration(classes={PersistenceConfig.class})
您需要像这样指定一个 main 来启动您的应用程序以启用 Spring 启动自动配置:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
有关 JPA 存储库自动配置的详细信息,请参阅 JpaRepositoriesAutoConfiguration。
解决了将此添加到 PersistenceConfig class
@Bean
public DeviceEventRepository deviceEventRepository() {
return new JdbcDeviceEventRepository();
}