Spring Boot Cucumber 测试无法解析占位符“random.uuid" [英] Spring Boot Cucumber tests could not resolve placeholder 'random.uuid'

查看:53
本文介绍了Spring Boot Cucumber 测试无法解析占位符“random.uuid"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Spring Boot 属性在默认情况下无法猜测随机值(出于安全原因),所以我尝试使用 random UUID 作为默认值,使用类似的代码这个:

I want a Spring Boot property to have an impossible to guess random value by default (for security reasons), so I am trying to use a random UUID as the default value, using code like this:

@Service
public class UserServiceImpl implements UserService {
...
   @Autowired
   public UserServiceImpl(@NonNull final PasswordEncoder passwordEncoder,
            @NonNull final UserRepository userRepository,
            @NonNull @Value("${administrator.password:${random.uuid}}") final String administratorPassword) {
      ...
   }

但是我的 Cucumber Spring Boot 测试抱怨 ${random.uuid} 因此:

But my Cucumber Spring Boot tests are complaining about the ${random.uuid} thus:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in file [.../UserServiceImpl.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'random.uuid' in value "administrator.password:${random.uuid}"

我该怎么做才能让我的应用程序使用随机属性值?

What do I have to do to get my application to use a random property value?

推荐答案

问题可能与测试切片有关.如果我运行一个干净的 Spring Boot 项目的测试:

The problem might be related to test slicing. If I run a clean Spring boot project's test with:

@SpringBootTest
class DemoApplicationTests {

    @Value("${nonexistingValue:${random.uuid}}")
    private String someVal;

    @Test
    public void someTest() {
        assertThat(someVal).contains("-");
    }

}

测试通过.但是,如果我将 @SpringBootTest 更改为 @ExtendWith({SpringExtension.class})@RunWith(SpringRunner.class),则测试失败.${random.uuid} 和类似的表达式应该在正常的运行时环境中可用.

The test passes. However, if I change @SpringBootTest to @ExtendWith({SpringExtension.class}) or @RunWith(SpringRunner.class), the test fails. ${random.uuid} and similar expressions should be available in a normal runtime environment.

由于这种切片,似乎RandomValuePropertySource 不可用.一个相当不优雅的解决方法是使用在测试上下文中创建的 PropertySourcesPlaceholderConfigurer bean 将其显式添加到上下文中:

Because of this slicing, it seems that the RandomValuePropertySource is not available. A rather inelegant work-around is to explicitly add it to the context, using a PropertySourcesPlaceholderConfigurer bean created in your test context:

@Configuration
public class CucumberBeansConfiguration {

   @Bean
   public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      final var configurer = new PropertySourcesPlaceholderConfigurer();
      final var sources = new MutablePropertySources();
      sources.addFirst(new RandomValuePropertySource());
      configurer.setPropertySources(sources);
      return configurer;
   }

}

这篇关于Spring Boot Cucumber 测试无法解析占位符“random.uuid"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆