Skip to content

Instantly share code, notes, and snippets.

@cbeams
Created November 4, 2011 18:59
Show Gist options
  • Select an option

  • Save cbeams/1340178 to your computer and use it in GitHub Desktop.

Select an option

Save cbeams/1340178 to your computer and use it in GitHub Desktop.
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