spring boot aop junit

背景:目前系统中有一些接口,已经正在对外提供服务,这些接口全部是对人员的操作。现在想记录下操作日志,存于DB中。
想法:利用spring aop,对这些人员操作接口,做切面。由于只记录操作日志,因为使用AfterReturning即可。人员操作的接口一共有好几个,利用了aop之后,就不需要在人员操作的每个接口方法中手动添加方法,可以直接在aop的切面中完成。
问题:完成之后,想对spring aop进行单元测试,在这一步暂时卡住了。

下面一共有三个方法,问题1:为什么方法一,我尝试的时候,始终没有成功;问题2:这下面的三种方法,有什么差别,方法三我还没有尝试。

方法一

参考Spring、Spring Boot和TestNG测试指南 - 测试AOP,尝试下面的方法。

首先,遇到的一个问题是:Error:(36, 8) java: cannot access org.testng.IHookable class file for org.testng.IHookable not found,定位之后进入源码,还真发现没有对应的类,因此加上一个依赖jar包compile 'org.testng:testng:6.14.3'即可。

@SpringBootTest
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class AOPTest extends AbstractTestNGSpringContextTests {
    @Autowired
    private MemberService memberService;//service层

    @Autowired
    private UserRepository userRepository;//相当于是DAO层

   @SpyBean
    private MemberManageAspect memberManageAspect;//aop

    @Test
    public void testFooService() throws Throwable {
        User currentUser = userRepository.findOne("user_id", "111111");
        verify(memberManageAspect, times(1)).watchUpdateUser(currentUser);
    }

dubug时候,发现memberServiceuserRepositorymemberManageAspect都是null,不知道为什么。

然后在AOPTest类上加一个 @RunWith(SpringJUnit4ClassRunner.class)就可以发现memberServiceuserRepositorymemberManageAspect不是null了,但是报错:

Actually, there were zero interactions with this mock.
at com.****.AOPTest.testFooService(AOPTest.java:93)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
方法二

然后想了下,既然aop是可以自动进行切面注入的,那么我如果只是测试对应的service被调用后,aop是否执行了,也可以。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class AOPTest extends AbstractTestNGSpringContextTests {
    @Autowired
    private MemberService memberService;//service层

    @Autowired
    private UserRepository userRepository;//相当于是DAO层

   @SpyBean
    private MemberManageAspect memberManageAspect;//aop

    @Test
    public void testFooService() throws Throwable {
        User currentUser = userRepository.findOne("user_id", "111111");
        updateUser(currentUser);
    }

这样,也可以debug到aop中。注意,aop类的上面,也需要

@Component
@Aspect

这样的两个注解,否则aop无法自动装配。

方法三

除了上面两种方法之外,还看到第三种aop test方法,具体如JUnit tests for AspectJ

附:
aop官方
AOP中 @Before @After @AfterThrowing@AfterReturning的执行顺序

Spring、Spring Boot和TestNG测试指南 - 使用Mockito

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,使用AOP的环绕通知可以通过配置pom文件和编写测试类来实现。首先,在pom文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 这个依赖会自动开启自动代理,相当于使用了`@EnableAspectJAutoProxy`注解,因此不需要再手动添加这个注解。\[1\]\[2\] 接下来,可以编写一个使用Spring Boot单元测试的测试类。首先,导入需要的类: ``` import com.ljs.springbootplace.service.MyService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ``` 然后,在测试类上添加注解`@RunWith(SpringJUnit4ClassRunner.class)`和`@SpringBootTest(classes = SpringbootplaceApplication.class)`,指定运行的JUnit运行器和Spring Boot的启动类。同时,注入需要测试的服务类: ``` @Autowired private MyService myService; ``` 最后,编写测试方法,调用需要测试的方法: ``` @Test public void test() { String userNameById = myService.getUserNameById_testAspect(1);//测试aop System.out.println(userNameById); } ``` 这样就可以使用Spring BootAOP功能进行环绕通知的测试了。\[3\] #### 引用[.reference_title] - *1* [SpringBoot-AOP环绕通知记录日志/鉴权](https://blog.csdn.net/wjg8209/article/details/119675560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springboot实现aop环绕通知](https://blog.csdn.net/weixin_43421537/article/details/104778208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值