Saturday, June 22, 2013

Spring Testing 3.1: Mixing XML resources and @Configuration classes

Step 01
package com.resource.config;

public class User {
    private String userName;
    private Long salary;

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Step 02

package com.resource.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanApp {
    /**
     * Using property \'EL\' syntax to load values from the user.properties value
     */

    private @Value(\"#{userProperties[\'userBean.username\']}\")
    String userName;
    private @Value(\"#{userProperties[\'userBean.salary\']}\")
    Long salary;

    /*
     * Injected bean
     */

    @Bean(name = \"userBean\")
    public User addUser() {
        User user = new User();
        user.setUserName(userName);
        user.setSalary(salary);
        return user;
    }
}

Step 03

package com.resource.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class UserBeanMain {

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(
                UserBeanApp.class);
        User jetBean = ctx.getBean(User.class);
        System.out.println(\"User Name=\" + jetBean.getUserName());
        System.out.println(\"Salary=\" + jetBean.getSalary());
    }

}

Step 04 

package com.resource.config.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

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.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.resource.config.User;
import com.resource.config.UserBeanApp;

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from UserBeanApp
@ContextConfiguration(classes = { UserBeanApp.class })
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanTest {
    /**
     * Using property \'EL\' syntax to load values from the user.properties value
     */

    private @Value(\"#{userProperties[\'userBean.username\']}\")
    String userName;
    private @Value(\"#{userProperties[\'userBean.salary\']}\")
    Long salary;

    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(userName));
        assertThat(userBean.getSalary(), equalTo(salary));
    }
}
Dowload Source Code: spring-resource-config

No comments:

Post a Comment