Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于缺少依赖项,Spring boot junit test@JpaDataTest无法工作_Spring_Spring Boot_Junit_Spring Data Jpa - Fatal编程技术网

由于缺少依赖项,Spring boot junit test@JpaDataTest无法工作

由于缺少依赖项,Spring boot junit test@JpaDataTest无法工作,spring,spring-boot,junit,spring-data-jpa,Spring,Spring Boot,Junit,Spring Data Jpa,我正在尝试使用我的实际配置编写测试 在正常的spring上下文中,我使用一个从属性中解密数据库密码的@Autowired服务加载数据源 看起来像这样 @Configuration public class DataBaseConfig { @Value("${swat.datasource.url}") private String dbURL; @Value("${swat.datasource.driver-class-name}&q

我正在尝试使用我的实际配置编写测试

在正常的spring上下文中,我使用一个从属性中解密数据库密码的
@Autowired
服务加载数据源

看起来像这样

@Configuration
public class DataBaseConfig {

    @Value("${swat.datasource.url}")
    private String dbURL;

    @Value("${swat.datasource.driver-class-name}")
    private String driverName;

    @Value("${swat.datasource.username}")
    private String userName;

    @Value("${swat.datasource.password}")
    private String hashedPassword;

    @Autowired
    EncryptionService encryptionService;

    @Bean
    public DataSource primaryDataSource() {
        String password = encryptionService.decriptPassword(hashedPassword);
        return DataSourceBuilder.create().url(dbURL).driverClassName(driverName).username(userName).password(password).build();
    } 
我现在正在尝试使用@JpaDataTest(不是@SpringBootTest)运行测试 执行以下操作

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
@Import(DataBaseConfig.class)
@TestPropertySource(value = "file:./executor.properties")
public class NewDeviceTest {
    
    @Autowired
    NewDeviceService newDeviceService;
    
    @Test
    public void loadNewDevices() {
        List<NewDevice> devices = newDeviceService.findAll();
        assertEquals(1, devices.size());
        assertTrue(devices.get(0).isNew());
    }

}

但它加载了所有组件,其中一些组件的行为是我在测试中不希望看到的(加载初始数据库数据…调度等)

也许您可以使用这个注释@Order(Ordered.HIGHEST\u priority)no。。。我收到了相同的错误:没有类型为“com.xxxx.compliance.executor.dao.EncryptionService”的合格bean可用:预期至少有一个bean符合autowire候选
@Import(DataBaseConfig.class)
您只加载了一个类,这就是原因。@tomas我已经导入了数据库配置,但是此配置需要EncryptionService您提到的错误是由于需要添加一个bean名称以限定您需要使用的bean,所以@bean(name=firstEncryptionService”)然后您使用它@Autowired@Qualifier(“firstEncryptionService”)在导致此问题的地方您可以使用此注释@order(Ordered.HIGHEST_priority)否…我收到相同的错误:没有可用的“com.xxxx.compliance.executor.dao.EncryptionService”类型的合格bean:至少需要1个符合autowire候选
@Import(DataBaseConfig.class)条件的bean
您只加载了一个类,这就是原因。@tomas我已经导入了数据库配置,但此配置需要EncryptionService您提到的错误是因为需要添加一个bean名称来限定您需要使用的bean,所以@bean(name=firstEncryptionService),然后使用它@Autowired@Qualifier(“firstEncryptionService”)它在哪里引起了这个问题
@ComponentScan("com.wisemon.compliance.executor.service")