Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@value annotations working on spring junit test, annotation ONLY config. no xml
// ./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
@martian111

This comment has been minimized.

Copy link

@martian111 martian111 commented Jun 17, 2014

Thanks for this! This was helpful for me to solve my @value annotations not being processed issue. As I was implementing and testing this, I've discovered in the Spring documents the PropertySourcesPlaceholderConfigurer (ref: http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/PropertySource.html). Using this instead of PropertyPlaceholderConfigurer is better since it uses the @PropertySource annotations, and thus, does not need to be redefined. Also, the method that defines it should be static per Spring warnings in the log and http://stackoverflow.com/a/14943106/1034436.

Hope this improves your Gist!

@chinthakasajith

This comment has been minimized.

Copy link

@chinthakasajith chinthakasajith commented May 13, 2015

Is there any way to make autowire without using xml ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment