51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1508|回复: 0
打印 上一主题 下一主题

[原创] 浅谈AOP功能测试

[复制链接]
  • TA的每日心情
    擦汗
    昨天 09:00
  • 签到天数: 1025 天

    连续签到: 4 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-10-26 11:08:53 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
    一丶 基本概念
      AOP: [动态代理]
      指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。
      1丶导入 aop模块:Spring Aop: (spring-aspects)。
      2丶定义一个业务逻辑类(MathCalulcator);在业务逻辑运行的时候将日志进行打印(方法之前,方法运行结束丶方法出现异常)。
      3丶定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div
      通知方法:
      前置通知(@Before):logStart: 在目标方法(div)运行之前运行
      后置通知(@After):logEnd;在目标方法(div)运行结束之后运行(无论方法正常接结束还是异常结束)
      返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
      异常通知(@AfterThrowing):logexception:在目标方法(div)出现异常以后运行
      环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
      4、给切面类的目标方法标注何时何地运行(通知注解)。
      三步
      5丶将切面类和业务逻辑类(目标方法所在类)都加入到容器中。
      6、必须告诉Spring哪个是切面类,在切面类上加上注解。@Aspect
      7、给配置类中加@EnableAspectJAutoProxy 开启基于注解的切面模式。(Aop 模式)
      测试
      1 业务方法
    1. package com.mongoubiubiu.aop;

    2.   public class MathCalculator {

    3.           

    4.       public int div(int i,int j){

    5.           return i/j;

    6.       }

    7.   }
    复制代码
    2 切面类
    1. package com.mongoubiubiu.aop;

    2.   import java.util.Arrays;

    3.   import org.aspectj.lang.JoinPoint;

    4.   import org.aspectj.lang.annotation.After;

    5.   import org.aspectj.lang.annotation.AfterReturning;

    6.   import org.aspectj.lang.annotation.AfterThrowing;

    7.   import org.aspectj.lang.annotation.Aspect;

    8.   import org.aspectj.lang.annotation.Before;

    9.   import org.aspectj.lang.annotation.Pointcut;

    10.   import com.mchange.v1.util.ArrayUtils;

    11.   /**

    12.    * 切面类

    13.    * [url=home.php?mod=space&uid=267564]@Author[/url] 86138

    14.    *

    15.    */

    16.   @Aspect

    17.   public class LogAspects {

    18.       //抽取公共的切入点表达式

    19.       //1丶本类引用     在MathCalulator的任意方法 任意参数执行切入

    20.       @Pointcut("execution(public int com.mongoubiubiu.aop.MathCalculator.*(..))")

    21.       public void pointCut(){}

    22.       @Before("pointCut()")

    23.       public void logStart(JoinPoint joinpoint){

    24.           Object args=    joinpoint.getArgs();

    25.           System.out.println(joinpoint.getSignature().getName()+"名字运行。。。。。参数列表是:{"+Arrays.asList(args)+"}");

    26.       }

    27.      

    28.       @After("pointCut()")

    29.       public void logEnd(){

    30.           System.out.println("除法结束。。。。参数列表是:{}");

    31.       }

    32.      

    33.       //JoinPoint 一定要出现在参数表的第一位

    34.       @AfterReturning(value="pointCut()",returning="result")

    35.       public void logreturn(JoinPoint joinpoint,Object result){

    36.           System.out.println(joinpoint.getSignature().getName()+"名字运行正常返回。。。。结果是:{"+result+"}");

    37.       }

    38.      

    39.       @AfterThrowing(value="pointCut()",throwing="exception")

    40.       public void logexception(Exception exception){

    41.           System.out.println("除法异常信息。。。。异常信息:{"+exception+"}");

    42.       }

    43.      

    44.   }
    复制代码


    3 配置类

    1.  package com.mongoubiubiu.conf;

    2.   import org.aspectj.lang.annotation.Aspect;

    3.   import org.springframework.context.annotation.Bean;

    4.   import org.springframework.context.annotation.Configuration;

    5.   import org.springframework.context.annotation.EnableAspectJAutoProxy;

    6.   import com.mongoubiubiu.aop.LogAspects;

    7.   import com.mongoubiubiu.aop.MathCalculator;

    8.   /**

    9.    * AOP: [动态代理]

    10.    *       指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。

    11.    *

    12.    * 1丶导入 aop模块:Spring Aop: (spring-aspects)

    13.    * 2丶定义一个业务逻辑类(MathCalulcator);在业务逻辑运行的时候将日志进行打印(方法之前,方法运行结束丶方法出现异常,)

    14.    * 3丶定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div

    15.    *       通知方法:

    16.    *               前置通知(@Before):logStart: 在目标方法(div)运行之前运行

    17.    *               后置通知(@After):logEnd;在目标方法(div)运行结束之后运行(无论方法正常接结束还是异常结束)

    18.    *               返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行

    19.    *               异常通知(@AfterThrowing):logexception:在目标方法(div)出现异常以后运行

    20.    *               环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())

    21.    *4、给切面类的目标方法标注何时何地运行(通知注解)

    22.    *

    23.    *三步

    24.    *5丶将切面类和业务逻辑类(目标方法所在类)都加入到容器中

    25.    *6、必须告诉Spring哪个是切面类,在切面类上加上注解。@Aspect

    26.    *7、给配置类中加@EnableAspectJAutoProxy 开启基于注解的切面模式(Aop 模式)

    27.    *

    28.    *

    29.    *

    30.    *

    31.    */

    32.   @EnableAspectJAutoProxy

    33.   @Configuration

    34.   public class MainConfigOfAop {

    35.       //业务逻辑类加入容器中

    36.       @Bean

    37.        public MathCalculator mathCalculator(){

    38.            return new MathCalculator();

    39.        }

    40.        //切面类加入容器中

    41.       @Bean

    42.       public LogAspects logAspects(){

    43.           return new LogAspects();

    44.       }

    45.      

    46.   }
    复制代码


    4 Junit测试类

    1.  package com.mongougbiubiu.test;

    2.   import javax.sql.DataSource;

    3.   import org.junit.Test;

    4.   import org.springframework.context.annotation.AnnotationConfigApplicationContext;

    5.   import com.mongoubiubiu.aop.MathCalculator;

    6.   import com.mongoubiubiu.conf.MainConfigOfAop;

    7.   import com.mongoubiubiu.conf.MainConfigOfAutowired;

    8.   import com.mongoubiubiu.conf.MainConfigOfProfile;

    9.   public class IOC_Test_Aop {

    10.      

    11.       @Test

    12.       public void Test01(){

    13.           //创建ioc 容器

    14.           AnnotationConfigApplicationContext applica=    new AnnotationConfigApplicationContext(MainConfigOfAop.class);

    15.           MathCalculator clazz=    applica.getBean(MathCalculator.class);

    16.           

    17.           clazz.div(1, 0);

    18.           applica.close();

    19.       }

    20.   }
    复制代码


    5 结果



    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-27 10:19 , Processed in 0.064646 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表