Springmvc 测试类(注解)
来源:互联网 发布:win10美化mac os 编辑:程序博客网 时间:2024/06/09 22:02
springmvc的测试类主要用到的注解有:
- @ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class})
- @RunWith(SpringJUnit4ClassRunner.class)
- @TransactionConfiguration
- @WebAppConfiguration
- @ActiveProfiles("test")
- @Profile("test")
- @Configuration
在这里,先看@Profile("test")这个注解,如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。各种此类的需求,让我希望有一个简单的切换开发环境的好办法,spring3.1给我们带来了profile,可以方便快速的切换环境。他的xml配置方式也很简单,只要在applicationContext.xml中添加下边的内容,就可以了:
<beans profile="develop"> <context:property-placeholder location="classpath*:jdbc-develop.properties"/> </beans> <beans profile="production"> <context:property-placeholder location="classpath*:jdbc-production.properties"/> </beans> <beans profile="test"> <context:property-placeholder location="classpath*:jdbc-test.properties"/> </beans>而@Profile("test")这个注解,则是作用于类上,相当于一个
<beans profile="test">配合@Configuration注解,则可以将该类设置成一个配置类:
<pre name="code" class="java"><pre name="code" class="java">@Profile("test")@Configurationpublic class TestAppConfig{ @Autowired ConfigurableEnvironment environment; public @Bean OrderService orderService() { return new OrderService(orderRepository()); } ……}
以上部分我们可以看做是一个xml文件,如何转换,可以看http://blog.csdn.net/tanksyg/article/details/8556769也可以搜@Configuration注解的作用。
我们再来看看ConfigurableEnvironment 这个类。我们其实可以把它想象成一个环境配置类,我们将我们开发需要配置的资源环境如:特殊常量,url,缓存地址,文件服务器地址,第三方服务地址等配置在一个properties文件中,通过environment.getRequiredProperty("XXX")这样的方式,就可以获取到其中配置的值。而为什么要提这个类呢,我们看到上面xml文件中:
<beans profile="develop"> <context:property-placeholder location="classpath*:jdbc-develop.properties"/> </beans>
profile下配置了一个context 而其指向的正是一个properties文件。由此我们就相当于写完了一个配置类,里面放入了対玉特定环境(test,develop……)下需要的一个资源配置。
接下来我们看看一个测试类的注解:
@ActiveProfiles("test")@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class})@TransactionConfiguration@WebAppConfigurationpublic class SpringBaseTest { @Autowired ApplicationContext applicationContext; ……}
@ContextConfiguration即导入我们需要的配置文件或类,上面代码导入了Appconfig和TestAppConfig这两个配置类。Appconfig类作为一个主配置类,配置了spring及springmvc需要的信息。相当于我们的applicationContext.xml这个文件啦,而TestAppConfig则相当于一个include到applicationContext.xml中的一部分内容。
@RunWith的作用如下:
JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.
@ActiveProfiles("test")就很好理解了,他的属性“test”就告诉我们那个profile类是当前运用的啦。如这个测试类就是说test这个profile是我们要用的,而他就对应上面的TestAppConfig这个类哦。
关于PropertySource、Environment、Profile还可以参考这篇文章:http://blog.csdn.net/xwq911/article/details/50113121
@TransactionConfiguration用与管理spring事务的。
@WebAppConfiguration 他在测试环境使用,在package org.springframework.test.context.web;包下。用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的;value属性指定web应用的根;同时,他必须与@ContextConfiguration一同使用。
在这里也给出自己的一个猜测,就是为什么需要加@WebAppConfiguration因为在加入@RunWith(SpringJUnit4ClassRunner.class)注解后,会要求我们加载ApplicationContext(估计是SpringJunit4ClassRunner在类中需要用到),而如果我们不提供@WebAppConfiguration用于说明需要WebApplicationContext则会报
java.lang.IllegalStateException: Failed to load ApplicationContext这样的错误。而如果我们不用@RunWith(SpringJUnit4ClassRunner.class)的话,如果代码中有下面语句,
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();则会报
java.lang.IllegalArgumentException: WebApplicationContext is required
说明@RunWith(SpringJUnit4ClassRunner.class)的作用是给我们实例化了WebApplicationContext。
0 0
- Springmvc 测试类(注解)
- SpringMVC(2)-- 常用注解类
- SpringMVC 其他注解类
- Springmvc注解类详解
- SpringMVC注解(1)
- springmvc注解(1)
- SpringMVC(一)注解
- spring实用注解(二)--springMvc注解
- SpringMVC源码(九)SpringMVC注解开发
- 注解(增加类类型注解属性测试)
- springMVC model类注解注入
- springmvc的各种注解类
- 二.springMVC常用注解类
- SpringMVC注解入门(一)
- springMVC初学实例(注解)
- springMVC 注解版(基础)
- SpringMVC--常用注解(详解)
- springMVC-注解开发(上)
- Linux 任务定时命令crontab解析
- Uni2D 学习
- 通过SSH和proxyChain连接内网SVN
- [面试]牛人整理 操作系统、计算机网络、设计模式 、Linux、算法
- 字长、字节、Bit、Byte
- Springmvc 测试类(注解)
- Android intent常见应用总结
- iOS URL Scheme 使用详解
- 华为oj 超长正整数相加
- css自适应media规则
- 关于回车直接执行事件的方法
- 无领导小组讨论案例及其分析1
- 和大家分享一个老外的代码适用unity5.2.3以上
- 在activity,fragment中获得actionbar的高度