dus*_*ltz 5 java spring spring-boot
是否有可能以某种方式"存储"由其生成的随机值${random.value}并将其作为嵌套值引用到其他属性中?我似乎无法想象如何使这项工作.
以下是该方案的示例.但是,我希望以下测试能够通过,foo并bar始终解析为不同的随机值.
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by dustin.schultz on 12/14/15.
*/
@SpringApplicationConfiguration(classes = FooBar.TestApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "foo=${random.value}", "bar=${foo}" })
public class FooBar
{
@Value("${foo}")
private String foo;
@Value("${bar}")
private String bar;
@Test
public void foo()
{
assertThat(foo, is(bar));
}
@Configuration
@EnableAutoConfiguration
public static class TestApplication
{
}
}
Run Code Online (Sandbox Code Playgroud)