Skip to content

Instantly share code, notes, and snippets.

@anbestephen
Forked from danlangford/dan.langford.SomeBean.java
Last active August 29, 2015 14:24
Show Gist options
  • Save anbestephen/d7fa0597a98fbd7bbcf1 to your computer and use it in GitHub Desktop.
Save anbestephen/d7fa0597a98fbd7bbcf1 to your computer and use it in GitHub Desktop.
// ./src/main/java/dan/langford/SomeTest.java
package dan.langford;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SomeBean {
@Value("${some.value}")
private String someValue;
private String check = "test";
public String getSomeValue() {
return someValue;
}
public void setSomeValue(String someValue) {
this.someValue = someValue;
}
public String getCheck() {
return check;
}
public void setCheck(String check) {
this.check = check;
}
}
// ./src/test/java/dan/langford/SomeTest.java
package dan.langford;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SomeTest {
private static Log LOG = LogFactory.getLog(SomeTest.class);
@Value("${some.value}")
private String value;
@Autowired
private SomeBean bean;
@Test
public void testOne() {
LOG.error("What?? ONE "+this.value);
assertEquals("our value should be populated", "defenestration", this.value);
}
@Test
public void testTwo() {
LOG.error("What?? TWO "+bean.getSomeValue());
assertThat("we should be sane", "test", is(bean.getCheck()));
assertThat("bean value should be populated", "defenestration", is(bean.getSomeValue()));
}
@Configuration
@ComponentScan("dan.langford")
static class someConfig {
// because @PropertySource doesnt work in annotation only land
@Bean
PropertyPlaceholderConfigurer propConfig() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("some.properties"));
return ppc;
}
}
}
<!-- ./pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>annotationconfig</groupId>
<artifactId>annotationconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
# ./src/main/resources/some.properties
some.value=defenestration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment