Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发
Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发

一、环境配置

使用idea IDE 进行单元测试,首先需要安装JUnit 插件。
1.安装JUnit插件步骤 File-->settings-->Plguins-->Browse repositories-->输入JUnit-->选择JUnit Generator V2.0安装。

Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发
Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发
Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发
Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发
Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发

二.常用注解:

[email protected]: 测试方法
    a)(expected=XXException.class)如果程序的异常和XXException.class一样,则测试通过
    b)(timeout=100)如果程序的执行能在100毫秒之内完成,则测试通过
  [email protected]: 被忽略的测试方法:加上之后,暂时不运行此段代码
  [email protected]: 每一个测试方法之前运行
  [email protected]: 每一个测试方法之后运行
  [email protected]: 方法必须必须要是静态方法(static 声明),所有测试开始之前运行,注意区分before,是所有测试方法
  [email protected]: 方法必须要是静态方法(static 声明),所有测试结束之后运行,注意区分 @After 执行顺序:@BeforeClass=》@Before=》@Test=》@After=》@AfterClass
import org.junit.*;
import org.junit.Test;

public class Test1 {
    @Test
    public void test() {  System.out.println("HelloWorld!"); }
    @BeforeClass
    public static void beforeClass() { System.out.println("beforeClass");}
    @AfterClass
    public static void afterClass() {System.out.println("AfterClass");  }
    @Before
    public void before() { System.out.println("Before"); }
    @After
    public void After() { System.out.println("After");}
    @Ignore
    public void ignore() { System.out.println("ignore"); }
}

注意:编写测试类的原则:

①测试方法上必须使用@Test进行修饰
②测试方法必须使用public void 进行修饰(不能换其他修饰符如private,static),不能带任何的参数
③新建一个源代码目录来存放我们的测试代码,即将测试代码和项目业务代码分开
④测试类所在的包名应该和被测试类所在的包名保持一致
⑤测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖
⑥测试类使用Test作为类名的后缀(不是必须)
⑦测试方法使用test作为方法名的前缀(不是必须)

Junit+Spring

// 类的声明上方
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class Test2 {
    @Resource(name="user")// 成员位置
    User user = new User();
    @Test
    public void test2(){
        System.out.println(user.getName());
   		 }
    }

1.2.1Spring 使用 AspectJ 进行 AOP 的开发:注解的方式

1.2.1.1引入相关的 jar 包:
*spring 的传统 AOP 的开发的包
spring-aop-4.2.4.RELEASE.jar com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.2.4.RELEASE.jar
1.2.1.2引入 Spring 的配置文件

引入 AOP 约束:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"

1.2.1.3编写目标类:

public class ProductDao {
public void save(){ System.out.println("保存商品...");
}
public void update(){ System.out.println("修改商品...");
}
public void delete(){ System.out.println("删除商品...");
}

1.2.1.4配置目标类:

<!-- 目标类============ -->
<bean id="productDao" class="cn.vp.spring.demo4.ProductDao"></bean>

1.2.1.5开启 aop 注解的自动代理:

<aop:aspectj-autoproxy/>

1.2.1.6AspectJ 的 AOP 的注解:
Junit4测试入门、Spring 使用 AspectJ 进行 AOP 的开发
1.2.1.7编写切面类:

@Aspect
public class MyAspectAnno {


@Before("MyAspectAnno.pointcut1()")
public void before(){
System.out.println("前置通知===========");
}


@Pointcut("execution(* cn.vp.spring.demo4.ProductDao.save(..))")
private void pointcut1(){}
}

1.2.1.8配置切面:

<!-- 配置切面类 -->
<bean id="myAspectAnno" class="cn.vp.spring.demo4.MyAspectAnno"></bean>

1.2.1.9其他通知的注解:

@Aspect

public class MyAspectAnno {

@Before("MyAspectAnno.pointcut1()")
public void before(){
System.out.println("前置通知===========");
}

@AfterReturning("MyAspectAnno.pointcut2()")
public void afterReturning(){ System.out.println("后置通知===========");
}

@Around("MyAspectAnno.pointcut3()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
}

@AfterThrowing("MyAspectAnno.pointcut4()")
public void afterThrowing(){ System.out.println("异常抛出通知========");
}

@After("MyAspectAnno.pointcut4()")
public void after(){
System.out.println("最终通知==========");
}

@Pointcut("execution(* cn..spring.demo4.ProductDao.save(..))")
private void pointcut1(){}
@Pointcut("execution(* cn.vp.spring.demo4.ProductDao.update(..))")
private void pointcut2(){}
@Pointcut("execution(* cn.vp.spring.demo4.ProductDao.delete(..))")
private void pointcut3(){}
@Pointcut("execution(* cn.vp.spring.demo4.ProductDao.find(..))")
private void pointcut4(){}
}

相关文章:

  • 2021-10-17
  • 2021-10-19
  • 2022-12-23
  • 2021-11-27
  • 2022-02-03
  • 2021-08-22
猜你喜欢
  • 2022-01-06
  • 2022-12-23
  • 2021-07-03
  • 2021-12-09
  • 2021-11-07
  • 2021-04-16
  • 2021-06-14
相关资源
相似解决方案