Application of AOP in testing

1. Because the function code cannot be changed at will when testing the server or quasi production server verifies the development function, which causes a lot of time consumption, so we use aop of spring to simplify the test work. According to the requirements of the project tester, we need a single interface to realize the timeout when receiving the response. Our implementation idea is that after we receive the response from the three-party system, we use aop to return the response information to other nodes of the system and modify the response, so as to meet the test requirements.

This paper only provides a test idea. If you want to see the detailed introduction and use of aop, please move. Later, there will be time to update the aop related reception and use the configuration file to implement the aop function.

Technological process:

Code:

package com.ruim.ifsp.pafyhome.aop;

import com.ruim.ifsp.framework.utils.IfspSpringContextUtils;
import com.ruim.ifsp.log.IfspLoggerFactory;
import com.ruim.ifsp.pafyhome.bean.response.base.CommonTxnSimpleResponse;
import com.ruim.ifsp.pafyhome.dao.PayChannelDao;
import com.ruim.ifsp.utils.message.IfspFastJsonUtil;
import com.ruim.ifsp.utils.verify.IfspDataVerifyUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName PayhomeAspect
 * @Description
 * @Author Administrator
 * @Date 2020/3/4 16:24
 **/
@Aspect
@Component
@Order(200)
public class PayhomeAspect {
    //Definition log
    private Logger log = IfspLoggerFactory.getLogger(payhomeAspect.class);
    //Defining cut-off points
    //1. Radio and television test uses annotation to configure call method of pointcut GDPaymentServiceImp class.
    @Pointcut(value = "execution(public * com.ruim.ifsp.pafyhome.service.imp.GDPaymentServiceImp.call(..))")
    public void GDPay(){

    }
    //2. Social security test uses annotation to configure the pointcut PerBankPayInsurantChargeServiceImpl class call method
    @Pointcut(value = "execution(public * com.ruim.ifsp.pafyhome.service.imp.PerBankPayInsurantChargeServiceImpl.call(..))")
    public void SBPay(){

    }
    //3. Electricity payment test, not configured
    public void DFPay(){

    }
    /**
     *@author lxw
     *@date 2020/3/4
     *@desc Tangent method, using surround notification
     *@param arg
     *@return java.lang.Object
    **/
    @Around("GDPay()||SBPay()")
    public Object aspectMethod(ProceedingJoinPoint arg) throws Throwable {
        Object object = arg.proceed();
        try {
            log.info("============================Start processing payhomeAspect=================");
            Map<String,Object> paramsMap = IfspFastJsonUtil.objectTomap(arg.getArgs()[0]);
            String txnCode = IfspDataVerifyUtil.isEmptyMap(paramsMap)?"":(String) paramsMap.get("txnCode");
            PayChannelDao payChannelDao = IfspSpringContextUtils.getInstance().getBean(PayChannelDao.class);
            String preKey = payChannelDao.selectByPrimaryKey("PAYHOME_REJECT","303").getParamSysInfo();
            log.info("txnCode:{},preKey:{}",txnCode,preKey);
            if (preKey.equals("TRUE")){
                log.info("============================payhomeAspect timeout handler===========================");
                if ("303822".equals(txnCode)||"303922".equals(txnCode)){ //Radio and television and social security payment set to timeout
                    CommonTxnSimpleResponse resp = (CommonTxnSimpleResponse)object;
                    resp.setRespCode("3030003");
                    resp.setRespMsg("Host payment communication timeout, please launch query transaction query results later!");
                    return resp;
                }
            }
        }catch (Exception e){
            log.info("Handling exception:{}",e.getMessage());
            return object;
        }
        return object;
    }
}

It should be noted that:
1. When the controller calls the serviceimpl, first confirm whether the bean injection is interface injection or class injection.

1. Use class injection:  
@Resource(name = "aisleService")  
private AisleServiceImpl aisleService;  
  
2. Use interface injection:  
@Resource(name = "aisleService")  
private IAisleService aisleService; 

1. The dynamic agent injection of JDK cannot be used, because the dynamic agent of JDK does not support class injection, only interface mode injection;

2. jdk dynamic agent injection can be used;

If you want to use code 1, you must use the cglib proxy; otherwise, you will see: org.springframework.beans.factory.beannoftypeexception

This exception. You need to enable proxy < AOP: config proxy target class = "true" > in the spring configuration file.

2. The AOP class must register the bean in spring, otherwise it can't implement the function.

Published 9 original articles, won praise 3, visited 3374
Private letter follow

Tags: Spring JDK Java

Posted on Thu, 05 Mar 2020 00:32:33 -0800 by judgenules