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

  • 相关阅读:
    elementui组件库eldialog弹出框被遮罩层挡住
    python常规基础操作
    python中的字典排序
    python列表面试题
    python logging日志模块
    python序列之列表
    jmeter中csv连接数据库
    python必会的知识基础
    jmeter tcp 压力测试
    python模块基础知识练习
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/4035400.html
Copyright © 2011-2022 走看看