【发布时间】:2021-09-18 05:01:47
【问题描述】:
我有一个 Spring bean,它从 application.yml 读取配置属性值
public class AutoDisableRolesService {
@Value("${cron.enabled}")
private boolean runTask;
// remainder of class omitted
}
在 application.yml 中这个属性设置为 false
cron:
enabled: false
但是当我运行测试时,我希望它是真的。我尝试了以下方法,但它似乎不起作用
@SpringBootTest(properties = { "cron.enabled=true" })
@ExtendWith(MockitoExtension.class)
public class AutoDisableRolesServiceTests {
@Mock
private UserRoleRepository userRoleRepository;
@InjectMocks
private AutoDisableRolesService autoDisableRolesService;
// remainder of test class omitted
}
我也尝试了以下方法,但没有成功
@ContextConfiguration(classes = AutoDisableRolesService.class)
@TestPropertySource(properties = "cron.enabled=true")
@ExtendWith(MockitoExtension.class)
public class AutoDisableRolesServiceTests {
@Mock
private UserRoleRepository userRoleRepository;
@InjectMocks
private AutoDisableRolesService autoDisableRolesService;
// remainder of test class omitted
}
【问题讨论】:
-
@Michiel 您是否有机会将其充实为答案?
-
我看错了你最初的问题,它与测试属性无关。
标签: java spring spring-boot unit-testing junit