简体   繁体   English

测试 Spring bean 时模拟配置属性

[英]Mocking a configuration property when testing a Spring bean

I have a Spring bean that reads a configuration property value from application.yml我有一个 Spring bean,它从application.yml读取配置属性值

public class AutoDisableRolesService {

    @Value("${cron.enabled}")
    private boolean runTask;
    // remainder of class omitted
}

In the application.yml this property is set to false在 application.yml 中,此属性设置为 false

cron:
  enabled: false

But when I run the test, I want it to be true.但是当我运行测试时,我希望它是真的。 I've tried the following, but it does not seem to work我尝试了以下方法,但似乎不起作用

@SpringBootTest(properties = { "cron.enabled=true" })
@ExtendWith(MockitoExtension.class)
public class AutoDisableRolesServiceTests {
    
    @Mock
    private UserRoleRepository userRoleRepository;

    @InjectMocks
    private AutoDisableRolesService autoDisableRolesService;
    // remainder of test class omitted
}

I've also tried the following, without success我也尝试了以下方法,但没有成功

@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
}

You're mixing up two types of test set ups;您正在混合两种类型的测试设置; A Spring boot test setup with a Mockito test set up.带有 Mockito 测试设置的 Spring 启动测试设置。 By using @InjectMocks on your class under test, Mockito instantiates the class and injects all the fields annotated with @Mock , bypassing the Spring TestApplicationContext setup.通过在被测类上使用@InjectMocks ,Mockito 实例化该类并注入所有用@Mock注释的字段,绕过 Spring TestApplicationContext 设置。

Either use a Spring test set up using:使用以下方法设置 Spring 测试:

@SpringBootTest(properties = { "cron.enabled=true" })
public class AutoDisableRolesServiceTests {
    
    @MockBean
    private UserRoleRepository userRoleRepository;

    @Autowired
    private AutoDisableRolesService autoDisableRolesService;

    // remainder of test class omitted
}

Or a mockito set up using:或使用以下方法设置的 mockito:

public class AutoDisableRolesServiceTests {
    
    @Mock
    private UserRoleRepository userRoleRepository;

    @InjectMocks
    private AutoDisableRolesService autoDisableRolesService;

    @BeforeEach
    public void setUp() {
        ReflectionTestUtils.setField(autoDisableRolesService, "runTask", true);
    }
}

[edit] [编辑]

If you don't need a full @SpringBootTest set up, use如果您不需要完整的 @SpringBootTest 设置,请使用

@ExtendWith(SpringExtension.class)
@TestPropertySource(properties={"cron.enabled=true"})
@ContextConfiguration(classes = { AutoDisableRolesService.class})
public class AutoDisableRolesServiceTests {
    
    @MockBean
    private UserRoleRepository userRoleRepository;

    @Autowired
    private AutoDisableRolesService autoDisableRolesService;

    // remainder of test class omitted
}

The difference between @SpringBootTest and @ExtendsWith(SpringExtension.class) is that a @SpringBootTest loads the full (test)ApplicationContext while the latter only loads a partial context, which is faster but doesn't include everything. @SpringBootTest 和@ExtendsWith(SpringExtension.class) 之间的区别在于@SpringBootTest 加载完整的(测试)ApplicationContext,而后者只加载部分上下文,这更快但不包括所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM