zoukankan      html  css  js  c++  java
  • spring-junit的标注总结

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

    @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

  • 相关阅读:
    塔防游戏 代码project as 分享
    iOS网络监控— BMReachability
    Oracle学习(五):多表查询
    Servlet之生命周期【入门版(刚開始学习的人必看)】
    mysql 流程函数 存储引擎 InnoDB简单特性
    js
    UVa 10245
    Ubuntu14.04怎样使用root登录
    leetCode(29):Happy Number
    WebService:asp.net类库中添加WebService引用出现问题解决方法
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/4035400.html
Copyright © 2011-2022 走看看