如果在测试类的类名上面添加了注解

@ContextConfiguration("meta/springConfigured.xml")

如何在标注了@Test的方法里面获取上面xml文件中的配置?

package aoptest;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; import aoptest.ShouldBeConfiguredBySpring; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)//必须有,如果去掉,下面的ctx就不能注入
@ContextConfiguration("/meta/springConfigured.xml")
public class TestAop { @Autowired
protected ApplicationContext ctx; @Test
public void aopTest(){
//第一种方法
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("meta/springConfigured.xml");
//ShouldBeConfiguredBySpring myObject = (ShouldBeConfiguredBySpring) context.getBean("configuredBean");
//System.out.println(myObject.getName()+",wj");
//AopOperation aopOperation = (AopOperation) context.getBean("aopOperationBean");
//aopOperation.queryOpLog();
//第二种方法
ShouldBeConfiguredBySpring myObject = (ShouldBeConfiguredBySpring) ctx.getBean("configuredBean");//必须是spring的bean才能拦截,自己定义的类springaop无法拦截
myObject.TestAop(); }
}

参考http://www.coderli.com/junit-spring-test-applicationcontext

spring-junit的标注总结的更多相关文章

  1. spring junit 做单元测试,报 Failed to load ApplicationContext 错误

    spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...

  2. spring + junit 测试

    spring + junit 测试 需要一个工具类 package com.meizu.fastdfsweb; import org.junit.runner.RunWith; import org. ...

  3. spring+junit单元测试

    <1>读取文件: 配置文件在classes下:locations = {"classpath*:/spring/applicationContext.xml"} 配置文 ...

  4. spring junit

    转载自 http://blog.csdn.net/funi16/article/details/8691575 在写单元测试的时候,一般是对数据库进行增删改查的操作,这个时候,如果之前删除了某条记录, ...

  5. Spring Junit 读取WEB-INF下的配置文件

    假设Spring配置文件为applicationContext.xml 一.Spring配置文件在类路径下面 在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面 ...

  6. Spring Junit集成测试

    例子如下: package com.junge.demo.spring; import static org.junit.Assert.assertEquals; import java.util.L ...

  7. maven+spring+junit测试要注意的事情

    使用maven方式创建webapp工程的资料网上一大堆,在这里也不详细说了.在创建完成之后,里面说到要转动态web工程时要切换为3.0版本,但是我本地切换不了,网上的方法好像也没用,暂时也没用到这块. ...

  8. JAVA框架 Spring junit整合单元测试

    一.准备工作 1:Junit的需要的jar包: 2.spring的整合的jar包:spring-test-4.2.4.RELEASE.jar 3.代码实现 1) //导入整合的类,帮我们加载对应的配置 ...

  9. Spring+Junit测试用例的使用

    1.[导包]使用Spring测试套件,需要两个jar包:junit-X.X.jar和spring-test-X.X.X.RELEASE.jar,在maven项目下可添加如下依赖: <depend ...

  10. Spring+Junit+Mock测试web项目,即Controller

    准备:Maven依赖 <!-- Spring和MVC的包这里不列出来了,webmvc,aspects,orm,其他maven会自动导 --> <dependency> < ...

随机推荐

  1. c++内存管理错误记录

    extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer( const void * pUserData ){ if (!pUse ...

  2. JS之路——日期函数

    时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...

  3. UE标签排列方式

        UE是一款很顺手的文件编辑软件.打开文本,都会有个标签.根据个人喜好,我喜欢标签都展开,方便快速选取指定文本. 设置如下: 高级>配置>应用程序布局>可停靠窗口>类型 ...

  4. 汇顶指纹传感器GF919深度解析

    前言: 随着指纹识别技术的日益普遍,其在手机上的应用也得到了广泛关注.作为全球第一款Android正面按压指纹识别手机,魅族MX4 Pro所搭载的国产指纹识别系统可谓是赚足了眼球,这就是由汇顶科技提供 ...

  5. 一张图看懂片式多层陶瓷电容器(MLCC)

  6. Delphi应用程序的调试(十)调试器选项(在IDE中不要使用异常)

    可在两个级别上设置调试选项:工程级和环境级.在前面的讲解中讲解了工程级调试选项,通过主菜单[Project | Options…]打开如下对话框: 可在Debugger Options对话框中设置全局 ...

  7. Java Tomcat SSL 服务端/客户端双向认证

    借花献佛:http://www.blogjava.net/icewee/archive/2012/06/04/379947.html

  8. 【规律】【贪心】【数学】HDU 5573 Binary Tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 题目大意: 从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...

  9. js编写验证码

    这是一个简单的js编写的验证码,自己已经亲自验证,没有问题了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  10. 谷歌 google

    google Google是搜索引擎名,也是一家美国上市公司名称.Google公司于1998年9月7日以私有股份公司的形式创立,以设计并管理一个互联网的搜索引擎.Google公司的总部称作“Googl ...