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

  • 相关阅读:
    android intent 传递list或者对象
    MyEclipse快捷键大全
    keystore 介绍
    oracle存储过程学习---包的概念
    判断变量类型
    Android自定义控件之TextView
    Myeclipse SVN 修改用户名和密码
    关于Inflater
    windowsxp系统下SVN添加新用户
    【原创】python:open函数的使用方法
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/4035400.html
Copyright © 2011-2022 走看看