【问题标题】:Mocking a configuration property when testing a Spring bean在测试 Spring bean 时模拟配置属性
【发布时间】: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
}

【问题讨论】:

  • 尝试使用@TestPropertySource
  • @Michiel 您是否有机会将其充实为答案?
  • 我看错了你最初的问题,它与测试属性无关。

标签: java spring spring-boot unit-testing junit


【解决方案1】:

您混淆了两种类型的测试设置;带有 Mockito 测试设置的 Spring 引导测试设置。通过在您的测试类上使用@InjectMocks,Mockito 实例化该类并注入所有带有@Mock 注释的字段,绕过 Spring TestApplicationContext 设置。

使用 Spring 测试设置:

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

    @Autowired
    private AutoDisableRolesService autoDisableRolesService;

    // remainder of test class omitted
}

或使用以下方式设置的模拟:

public class AutoDisableRolesServiceTests {
    
    @Mock
    private UserRoleRepository userRoleRepository;

    @InjectMocks
    private AutoDisableRolesService autoDisableRolesService;

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

[编辑]

如果您不需要完整的 @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
}

@SpringBootTest 和@ExtendsWith(SpringExtension.class) 的区别在于@SpringBootTest 加载完整的(测试)ApplicationContext 而后者只加载部分上下文,这更快但不包含所有内容。

【讨论】:

  • 在使用第三种方法时,我将如何注入 AutoDisableRolesService 并模拟其依赖关系?
  • 非常感谢您的帮助,您能否添加一两行来解释SpringBootTest 和SpringExtension 方法之间的区别?
  • 看来您可以使用@SpringBootTest(classes = {AutoDisableRolesService.class}) 或将@SpringBootTest 与@ContextConfiguration(classes = { AutoDisableRolesService.class}) 结合使用来加载部分应用程序上下文。我想知道这些方法和@ExtendWith(SpringExtension.class)有什么区别吗?
  • 不同之处在于,虽然您提到的两种设置都只包含给定的类,但 SpringBootTest 还加载了您在普通 ApplicationContext 中拥有的额外配置(也称为测试切片)。例如,在非 SpringBootTest 中,PropertySourcesPlaceholderConfigurer 不会自动加载。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多