Sping单元测试简单示例

简介:

测试代码:

 

package com.yanek.ioc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.yanek.util.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestIOC extends UnitTestBase {

	public TestIOC() {
		super("classpath*:spring-ioc.xml");
	}
	
	@Test
	public void testSay1() {
		OneInterface oneInterface = super.getBean("oneInterface");
		oneInterface.say("This is a test1.");
	}
	@Test
	public void testSay2() {
		OneInterface oneInterface = super.getBean("oneInterface");
		oneInterface.say("This is a test2.");
	}

}


相关类和配置文件

package com.yanek.util;


import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UnitTestBase {
	
	private ClassPathXmlApplicationContext context;
	
	private String springXmlpath;
	
	public UnitTestBase() {}
	
	public UnitTestBase(String springXmlpath) {
		this.springXmlpath = springXmlpath;
	}
	
	@Before
	public void before() {
		
		if ((springXmlpath!=null && !springXmlpath.equals(""))) {
			springXmlpath = "classpath*:spring-*.xml";
		}
		try {
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}
	
	@After
	public void after() {
		context.destroy();
	}
	
	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId) {
		try {
			return (T)context.getBean(beanId);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected <T extends Object> T getBean(Class<T> clazz) {
		try {
			return context.getBean(clazz);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}

}


 

package com.yanek.ioc;

public interface OneInterface {
	
	public void say(String arg);
	
}

package com.yanek.ioc;

public class OneInterfaceImpl implements OneInterface {
	
	public void say(String arg) {
		System.out.println("ServiceImpl say: " + arg);
	}

}


spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
        
	<bean id="oneInterface" class="com.yanek.ioc.OneInterfaceImpl"></bean>
	
 </beans>


 

目录
相关文章
|
8天前
|
网络协议 安全 测试技术
性能工具之emqtt-bench BenchMark 测试示例
【4月更文挑战第19天】在前面两篇文章中介绍了emqtt-bench工具和MQTT的入门压测,本文示例 emqtt_bench 对 MQTT Broker 做 Beachmark 测试,让大家对 MQTT消息中间 BenchMark 测试有个整体了解,方便平常在压测工作查阅。
146 7
性能工具之emqtt-bench BenchMark 测试示例
|
8天前
|
JSON 数据可视化 测试技术
性能测试之Artillery(示例及指标)
性能测试之Artillery(示例及指标)
25 2
|
8天前
|
传感器 数据处理
示例三、光照度测试仪
示例三、光照度测试仪
19 1
|
8天前
|
SQL 测试技术 网络安全
Python之SQLMap:自动SQL注入和渗透测试工具示例详解
Python之SQLMap:自动SQL注入和渗透测试工具示例详解
30 0
|
7月前
|
SQL 测试技术
软件测试|深入理解SQL FULL JOIN:语法、用法及示例解析
软件测试|深入理解SQL FULL JOIN:语法、用法及示例解析
39 0
|
7月前
|
SQL 数据挖掘 测试技术
软件测试|深入理解SQL RIGHT JOIN:语法、用法及示例解析
软件测试|深入理解SQL RIGHT JOIN:语法、用法及示例解析
48 0
|
10月前
|
敏捷开发 IDE 测试技术
单元测试PHPUnit初体验之安装与示例
啥?你是程序员没写过单元测试?呃,好吧。小马这次也是需要支持CI/CD才刚开始强制自我实行。
98 0
单元测试PHPUnit初体验之安装与示例
|
10月前
|
Python
基于远程服务器的几种python服务框架示例及压力测试演示
基于远程服务器的几种python服务框架示例及压力测试演示
58 0
|
11月前
|
数据采集
详情接口测试及展示示例
详情接口测试及展示示例
103 0
详情接口测试及展示示例
|
测试技术 Go
Golang:testing单元测试的使用示例
Golang:testing单元测试的使用示例
62 0
Golang:testing单元测试的使用示例

热门文章

最新文章