Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
@value annotations working on spring junit test, annotation ONLY config. no xml
// ./src/main/java/dan/langford/
package dan.langford;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
public class SomeBean {
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/
package dan.langford;
import static;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class SomeTest {
private static Log LOG = LogFactory.getLog(SomeTest.class);
private String value;
private SomeBean bean;
public void testOne() {
LOG.error("What?? ONE "+this.value);
assertEquals("our value should be populated", "defenestration", this.value);
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()));
static class someConfig {
// because @PropertySource doesnt work in annotation only land
PropertyPlaceholderConfigurer propConfig() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource(""));
return ppc;
<!-- ./pom.xml -->
<project xmlns="" xmlns:xsi=""
# ./src/main/resources/

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: 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

Hope this improves your Gist!


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