Docking a Property File Using Mockito in Spring

I am trying to write unit test for the next method in my controller.

@Autowired
    private ApplicationContext context;

    private String getProperty() {
        try {
            Properties props = context.getBean("myProperties", Properties.class);
            String val = props.getProperty("myProperty");
......

Bean is declared like this in my context application:

<util:properties id="myProperties" scope="prototype" location="file:${catalina.base}/webapps/myProperties.properties"/>

How can I mock this so that I can test different values โ€‹โ€‹of the variable val?

I was thinking of creating a test properties file and taunting this:

context = Mockito.mock(ApplicationContext.class);
Mocikto.when(context.getBean("myProperties", Properties.class)).thenReturn(some test file)

but then I would have to declare the test file as a Bean somewhere.

I was wondering if there is an easier way to do this?

thank

+5
source share
3 answers

If you use spring -3 you can do:

<context:property-placeholder location="myprops.properties" />

And in your code:

@Value("${myProperty}")
private String myProp;

public String getMyProp() {
    return myProp;
}

, myprops.properties ${...}, @Value . unit test myProp.

+5

- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer , spring . PropertyPlaceholderConfigurer bean . Mockito, , .

, xml:

<context:property-placeholder 
  location="file:${catalina.base}/webapps/myProperties.properties"/>

( , , ):

 <bean id="whateverMyControllerIdIs" class="com.initech.foobar.MyControllerImpl">
   <property name="quux"><value>${myProperty}</value></property>
 </bean>

, , setter, :

String quux;

public void setQuux(String quux) {this.quux = quux;}

spring 3.1 , xml:

    @Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
    @Autowired
    Environment env;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(env.getProperty("testbean.name"));
        return testBean;
    }
}
+1

, , Spring . Config . :

1) Spring

2) , ,

3) getter() (.. Date)

4) "" , , Java (.. public static final PROP_XXX)

@Component
public class Config {

public static final String PROP_USER_NAME = "user.name";

private Properties applicationProperties;


/*** Functional methods ***/

/**
 * Helper method to ensure consistent Exception handling where requested property values are missing
 * from the properties files and they are "required" for the application to function correctly.
 *
 * @param key
 * @return The String value of the property requested
 */
private String readPropertyRequired(String key) {

    String value = readProperty(key);

    if(StringUtils.isBlank(value))  {
        throw new PropertyNotFoundException(key);
    }

    return value;
}

/**
 * Helper method to return String values from the properties files that have been loaded
 *
 * @param key
 * @return The String value of the property requested or NULL if empty
 */
private String readProperty(String key) {
    return applicationProperties.getProperty(key);
}


/*** Getters & Setters ***/

@Autowired
public void setApplicationProperties(Properties applicationProperties) {
    this.applicationProperties = applicationProperties;
}

public String getUserName() {
    return readPropertyRequired(PROP_USER_NAME);
}

}

You can then unit test this class by simply entering standard java.util.Properties

+1
source

All Articles