基于SSM或者SSH的三种单元测试方式
2017-07-26 23:16
741 查看
今天总结一下三种单元测试方法,这个废话不多说,直接上代码。
第一种方式(通过配置文件+原始方式获取bean):
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yuan.service.impl.TeacherServiceImpl;
public class TeacherServiceTest {
@Test
public void testTeacher(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-yuan-service.xml");
TeacherServiceImpl bean = (TeacherServiceImpl)ctx.getBean("teacherService");
System.out.println(bean.queryAll());
}
}
第二种方式(通过配置文件+IOC注入bean):
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import com.yuan.service.TeacherService;
/**
* 两种测试方法
* 类上边的两个注解都是需要的, 缺一不可
* @author yhl
*
*/
@RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-yuan-service.xml")
public class SpringTest {
@Resource
public TeacherService teacherService;
@Test
public void test1(){
System.out.println(teacherService.queryAll());
}
}
第三种方式(通过配置文件+IOC注入bean+继承AbstractJUnit4SpringContextTests):
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration(locations = {"classpath:spring-config-test.xml"})
public class AppTest extends AbstractJUnit4SpringContextTests{
@Resource
public TeacherService teacherService;
@Test
public void test1(){
System.out.println(teacherService.queryAll());
}
}
配置文件其实没什么说的,简单来说就是,自动扫描包,还有数据库连接信息,这些自己配置吧。
还要jar,这个测试主要是junit-4.9.jar和spring-test-3.2.0.RELEASE.jar。
第一种方式(通过配置文件+原始方式获取bean):
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yuan.service.impl.TeacherServiceImpl;
public class TeacherServiceTest {
@Test
public void testTeacher(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-yuan-service.xml");
TeacherServiceImpl bean = (TeacherServiceImpl)ctx.getBean("teacherService");
System.out.println(bean.queryAll());
}
}
第二种方式(通过配置文件+IOC注入bean):
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import com.yuan.service.TeacherService;
/**
* 两种测试方法
* 类上边的两个注解都是需要的, 缺一不可
* @author yhl
*
*/
@RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-yuan-service.xml")
public class SpringTest {
@Resource
public TeacherService teacherService;
@Test
public void test1(){
System.out.println(teacherService.queryAll());
}
}
第三种方式(通过配置文件+IOC注入bean+继承AbstractJUnit4SpringContextTests):
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration(locations = {"classpath:spring-config-test.xml"})
public class AppTest extends AbstractJUnit4SpringContextTests{
@Resource
public TeacherService teacherService;
@Test
public void test1(){
System.out.println(teacherService.queryAll());
}
}
配置文件其实没什么说的,简单来说就是,自动扫描包,还有数据库连接信息,这些自己配置吧。
还要jar,这个测试主要是junit-4.9.jar和spring-test-3.2.0.RELEASE.jar。
相关文章推荐
- ssh/ssm使用CXF实现基于Rest方式的WebService并返回json数据
- 基于Dockerfile的方式创建SSH服务镜像。
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- Android HAL实现的三种方式(3) - 基于Manager的HAL设计
- Android HAL实现的三种方式(1) - 基于JNI的简单HAL设计
- Centos远程登录三种方式telnet,ssh,vnc
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- 基于Linux的USB主/从设备之间的三种通信方式-转
- Android HAL实现的三种方式(1) - 基于JNI的简单HAL设计
- 基于全注解方式的SSH基础框架
- Android HAL实现的三种方式(3) - 基于Manager的HAL设计
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- ssm整合(基于xml配置方式)
- 获取某个类或者某个实例的Class的三种方式
- Android HAL实现的三种方式(2) - 基于Service的HAL设计
- Android HAL实现的三种方式(2) - 基于Service的HAL设计
- SSH基于口令及密钥方式的远程登陆简单实现
- 实现ssh自动登陆的三种方式
- 基于ssm框架对mybatis映射文件的多对一返回类型的junit4单元测试