constructor-arg和property的单元测试?

/ 0评 / 0

emmmm..来一篇2018年的笔记吧.

小弟水平不高...

嗯..

主要是测试一下constructor-arg和property的使用示例笔记?


文件清单

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.glj</groupId>
  <artifactId>springTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
	</dependency>
  	<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<version>1.9.5</version>
			<scope>test</scope>
	</dependency>
	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.3.RELEASE</version>
			<scope>test</scope>
	</dependency>
	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.3.RELEASE</version>
	</dependency>
  </dependencies>
</project>

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	<bean id="employee" name="employee" class="com.glj.pojo.Employee" autowire="default">
	<!-- property:通过setter对应的方法注入。 -->
		<property name="age" value="21"></property>
		<property name="name" value="glj"></property>
		<!-- 
		constructor-arg:通过构造函数注入。
		 <constructor-arg name="age" value="21"></constructor-arg>
		 <constructor-arg name="name" value="glj"></constructor-arg>
		 -->
	</bean>
</beans>

constructor-arg:通过构造函数注入。

property:通过setter对应的方法注入。

Employee.java

package com.glj.pojo;

public class Employee {
	private Integer age;
	private String name;
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Employee [age=" + age + ", name=" + name + "]";
	}
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(Integer age, String name) {
		super();
		this.age = age;
		this.name = name;
	}
	
}

使用springframework提供的单元测试

EmpolyeeTest.java

package com.glj.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.glj.pojo.Employee;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:ApplicationContext.xml"})
public class EmpolyeeTest {
	@Autowired
	ApplicationContext applicationContext;
	
	@Test
	public void testEmployee(){
		Employee employee = (Employee) applicationContext.getBean("employee");
		assertEquals(employee.getName(),"glj");
		System.out.println(employee.getName());
	}
}

其中:

@RunWith(SpringJUnit4ClassRunner.class)是junit4里用它来做junit加载器

@ContextConfiguration(locations={"classpath:ApplicationContext.xml"}) 主要用来加载spring的配置文件路径:是一个字符串数组,可以加载多个spring配置文件...


发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注