本文主要是介绍Spring测试代码讲解示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1 Spring测试简介
- 2 Spring运行项目测试
- 2.1 pom.xml
- 2.2 测试实例bean
- 2.3 测试配置
- 2.4 测试运行类
- 3 不运行项目测试
- 3.1 ClassPathXmlApplicationContext
- 3.2 AnnotationConfigApplicationContext
1 Spring测试简介
测试时开发工作中不可缺少的部分,单元测试只针对当前开放的类和方法进行测试,可以简单通过模拟依赖来实现,对运行环境没有依赖,但是仅仅进行单元测试是不够的,它只能验证当前类或方法能否正常工作
Spring
通过Spring TestContext Framework
对基础测试提供顶级支持,它不依赖于特定的测试框架,可以使用junit
,或者TestNG
Spring
提供了一个SpringJUnit4ClassRunner
类,它提供了Spring TestContext Framework
的功能。通过@ContextConfiguration
来配置Application Context
,通过@ActiveProfiles
确定活动的profile
2 Spring运行项目测试
2.1 pom.xml
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.9.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>
2.2 测试实例bean
package cn.jzh.test;public class TestBean {private String msg;public TestBean(String msg){this.msg= msg;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}
}
2.3 测试配置
package cn.jzh.test;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;@Configurationpublic class TestConfig {@Bean@Profile("dev")public TestBean devTestBean(){return new TestBean("from dev profile");}@Bean@Profile("prod")public TestBean prodTestBean(){return new TestBean("from prod profile");}
}
2.4 测试运行类
package cn.jzh.test;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;@RunWith(SpringJUnit4ClassRunner.class)//SpringJUnit4ClassRunner在junit环境下提供Spring TestContext Framework的功能
@ContextConfiguration(classes = {TestConfig.class})//用来加载配置applicationContext,其中classes属性用来加载配置类
@ActiveProfiles("dev")//用来声明活动的profile
public class TestMain {@Autowiredpublic TestBean bean;@Testpublic void pordBean(){String msg = bean.getMsg();System.out.println(msg);}
}
3 不运行项目测试
配置文件使用xml的话,就用ClassPathXmlApplicationContext
读取配置文件来测试,如果用注解的话,就用AnnotationConfigApplicationContext
3.1 ClassPathXmlApplicationContext
在src下新建配置文件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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="userinfoJdbcDao" class="cn.test.dao.impl.UserinfoJdbcDaoImpl"></bean> <bean id="userinfoHibDao" class="cn.test.dao.impl.UserinfoHibDaoImpl"></bean> <bean id="userinfoService" class="cn.test.service.impl.UserinfoServiceImpl"><property name="userinfoDao" ref="userinfoHibDao"></property></bean>
</beans>
测试
public class Test {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");UserinfoService userinfoService = applicationContext.getBean(UserinfoService.class,"userinfoService");Userinfo user = new Userinfo();user.setUserId(1);user.setUserName("test");user.setUserPass("test");userinfoService.save(user);}
}
3.2 AnnotationConfigApplicationContext
@Configuration
@ComponentScan("cn.jzh.aop")
@EnableAspectJAutoProxy//开启spring对aspectj代理的支持
public class AopConfig {
}
@Service
public class DemoMethodService {public void add(){System.out.println("service................");}
}
public class AopMain {public static void main(String[] args) {AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(AopConfig.class);DemoMethodService bean = acac.getBean(DemoMethodService.class);bean.add();acac.close();}
}
这篇关于Spring测试代码讲解示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!