Saturday, July 6, 2013

Spring Testing 3.1: Context Configuration Inheritance For XML

Step 01:

package com.config.inheritance.xml;

public class Department {
    private String departmentId;
    private String departmentName;

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

Step 02:

package com.config.inheritance.xml;

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 03: src/main/resources

"http://www.springframework.org/schema/beans\"
    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
    xmlns:context=\"http://www.springframework.org/schema/context\"
    xsi:schemaLocation=\"http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd\">
 
    "departmentBean\"
 class=\"com.config.inheritance.xml.Department\">
        "departmentId\"
 value=\"ID-01\"/>
        "departmentName\"
 value=\"ACCOUNT\"/>
    
    


Step 04: src/main/resources

"http://www.springframework.org/schema/beans\"
    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
    xmlns:context=\"http://www.springframework.org/schema/context\"
    xsi:schemaLocation=\"http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd\">
 
    "userBean\"
 class=\"com.config.inheritance.xml.User\">
        "userName\"
 value=\"Nguyen Trung Hieu\"/>
        "salary\"
 value=\"15000000\"/>
    
    


Step 05:src/test/java

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

import com.config.inheritance.xml.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = \"classpath:userContext.xml\")
public class UserBeanTest {

    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu\"));
        assertThat(userBean.getSalary(), equalTo(15000000L));
    }
}

Step 06:src/test/java

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

import com.config.inheritance.xml.Department;

@ContextConfiguration(locations = \"classpath:departmentContext.xml\")
public class DepartmentBeanTest extends UserBeanTest {
    @Autowired
    Department departmentBean;

    @Test
    public void testAddDepartment() {
        assertThat(departmentBean.getDepartmentId(), equalTo(\"ID-01\"));
        assertThat(departmentBean.getDepartmentName(), equalTo(\"ACCOUNT\"));
    }
}

Source Code

No comments:

Post a Comment