Thursday, June 27, 2013

Spring Testing 3.1: Active Enviroment Profile For XML

Step 01:

package com.config.enviroment.profile.xml;

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

    public String getUserName() {
        return userName;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getSalary() {
        return salary;
    }

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



Step 02:src/test/java

package com.config.enviroment.profile.xml.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.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.enviroment.profile.xml.User;

/**
 * For development enviroment
 * 
 * @author hieunt29
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from applicationContext.xml\"
@ContextConfiguration(locations = \"classpath:applicationContext.xml\")
//DEV environment will be actived
@ActiveProfiles(\"DEV\")
public class UserBeanDevTest {
    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu DEV\"));
        assertThat(userBean.getSalary(), equalTo(15000000L));
        assertThat(userBean.getEmail(), equalTo(\"DEV-hieunt29@fsoft.com.vn\"));
    }
}



Step 03:src/test/java

package com.config.enviroment.profile.xml.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.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.enviroment.profile.xml.User;
/**
 * For production enviroment
 * @author hieunt29
 *
 */

@RunWith(SpringJUnit4ClassRunner.class)
//ApplicationContext will be loaded from applicationContext.xml\"
@ContextConfiguration(locations = \"classpath:applicationContext.xml\")
//PROD environment will be actived
@ActiveProfiles(\"PROD\")
public class UserBeanProdTest {
    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu PROD\"));
        assertThat(userBean.getSalary(), equalTo(9999999l));
        assertThat(userBean.getEmail(), equalTo(\"PRODhieunt29@fsoft.com.vn\"));
    }
}

No comments:

Post a Comment