Created
November 4, 2011 18:59
-
-
Save cbeams/1340178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static org.hamcrest.CoreMatchers.equalTo; | |
| import static org.junit.Assert.assertThat; | |
| import org.junit.Test; | |
| import org.springframework.beans.factory.annotation.Value; | |
| import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
| import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; | |
| import org.springframework.mock.env.MockEnvironment; | |
| public class QuickTest { | |
| @Test | |
| public void withProperty() { | |
| AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); | |
| ctx.register(PropertySourcesPlaceholderConfigurer.class, Foo.class); | |
| ctx.setEnvironment(new MockEnvironment().withProperty("myprop", "someval")); | |
| ctx.refresh(); | |
| assertThat(ctx.getBean(Foo.class).myprop, equalTo("someval")); | |
| } | |
| @Test | |
| public void withoutProperty() { | |
| AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); | |
| ctx.register(PropertySourcesPlaceholderConfigurer.class, Foo.class); | |
| ctx.refresh(); | |
| assertThat(ctx.getBean(Foo.class).myprop, equalTo("defaultval")); | |
| } | |
| } | |
| class Foo { | |
| @Value("${myprop:defaultval}") | |
| String myprop; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment