java - Java Spring @autowire 在测试类中不起作用
问题描述
我有一个自动装配 CatalogDao 类的测试类。但是 CatalogDao 类不是自动装配的。值为空;
测试班
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {PersistenceConfig.class})
public class CatalogDaoIT {
@Autowired
private CatalogDao catalogDao;
@Test
public void saveCatalog_readSame_foundOne() {
// arrange
Catalog catalog = new Catalog();
配置类
package ch.matica.platform.persistence.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = "ch.matica.platform.persistence")
public class PersistenceConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}
DAO 类
package ch.matica.platform.persistence;
import java.util.Collection;
...
@Repository
public class CatalogDao {
...
解决方案
对我来说这有效
@SpringBootTest
public class CatalogDaoIT {
....
}
推荐阅读
- elixir - Elixir 内置组合方法?
- r - 将形状文件导入 R,多边形的多个实例 - 如何删除或简化多个多边形?
- html - HTML5 _ 如何向视频标记添加超链接
- bash - 你可以在没有 bash -c 的情况下将环境变量传递给 kubectl exec 吗?
- reactjs - 更改 URL 时如何在导航栏中添加/删除新选项卡?
- maps - CodenameOne:无法显示谷歌地图
- r - 使用 apply 函数加速嵌套 for 循环
- javascript - For-of 循环默认迭代器函数
- swift - Swift - 全局访问的类的依赖注入
- c# - 为什么不合并 2 个 pdf 文件?