Spring Boot不在测试中使用属性文件中的配置文件

时间:2016-06-09 05:52:11

标签: java spring spring-boot

在我的Spring Boot应用程序中,我有服务并测试它。当我运行测试时,它不使用test.properties文件中的配置文件属性,而是使用@ActiveProfiles注释。为什么Spring Boot没有从属性文件加载配置文件?

我从错误中看到了这一点,指出Spring找不到服务bean。即使我在test中明确使用服务实现类名称,也会发生这种情况:

@Autowired
SalaryIntervalsDBImpl salaryIntervalsDBImpl;

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'healthjobs.service.SalaryIntervalsDBImplTest': Injection of autowired de
pendencies failed; nested exception is org.springframework.beans.factory.BeanCre
ationException: Could not autowire field: healthjobs.service.SalaryIntervalsDBIm
pl healthjobs.service.SalaryIntervalsDBImplTest.salaryIntervalsDBImpl; nested ex
ception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No q
ualifying bean of type [healthjobs.service.SalaryIntervalsDBImpl] found for depe
ndency: expected at least 1 bean which qualifies as autowire candidate for this 
dependency. Dependency annotations: {@org.springframework.beans.factory.annotati
on.Autowired(required=true)}

所有其他属性都是从test.properties文件中获取的。

服务注释:

@Service
@Profile("SIFromDB")
public class SalaryIntervalsDBImpl implements SalaryIntervals{

测试注释:

@ActiveProfiles("SIFromDB") //If I remove this test will crash
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(App.class)
@TestPropertySource(locations="classpath:test.properties")
public class SalaryIntervalsDBImplTest {

test.properties文件:

spring.profiles.active=SIFromDB

0 个答案:

没有答案