Spring AOP - @Pointcut:@Test 方法的 @Before 建议不起作用

2024-01-02

我正在与:

  • Spring框架4.3.2
  • 方面J 1.8.9
  • JUnit
  • Gradle

该项目基于多模块。

In src/main/java (main) 我有一些@Aspect课程和他们按照预期的方式工作。我可以通过运行时和测试来确认

现在我需要 JUnit 通过日志显示@Test执行的方法名

因此在src/test/java (test)我有以下内容:

class TestPointcut {

    @Pointcut("execution(@org.junit.Test * *())")                         
    public void testPointcut(){}

}

@Aspect
@Component
public class TestAspect {

    private static final Logger logger = LoggerFactory.getLogger(TestAspect.class.getSimpleName());

    @Before(value="TestPointcut.testPointcut()")
    public void beforeAdviceTest(JoinPoint joinPoint){
        logger.info("beforeAdviceTest - Test: {} - @Test: {}", joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName() );
    }

}

观察第二节课有@Aspect and @Component因此它被Spring认可

Note: 我可以确认如果我写错了@Pointcut语法或表达式出现错误。

问题是当我执行我的@Test方法,对于TestAspect@Before建议永远不会起作用。

我在谷歌做了一项研究,我发现@Pointcut("execution(@org.junit.Test * *())")模式是正确的。 即使我使用更明确的方式,例如:@Pointcut(value="execution(public void com.manuel.jordan.controller.persona.*Test.*Test())"), 这是行不通的。

考虑我有以下内容Gradle

project(':web-27-rest') {
    description 'Web - Rest'
    dependencies {
       compile project(':web-27-service-api')

       testRuntime project(':web-27-aop')
       testRuntime project(':web-27-aop').sourceSets.test.output

缺少什么或有什么问题?

Alpha:

一种测试类是:

  • 服务器端与@Parameters and @ClassRule + @Rule

所以:

@RunWith(Parameterized.class)
@ContextConfiguration(classes={RootApplicationContext.class})
@Transactional
public class PersonaServiceImplTest {

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE= new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Autowired
    private PersonaService personaServiceImpl;

    ...

    @Parameters
    public static Collection<Persona[]> data() {
     .....
        });
    }

    ...

    @Test
    @Sql(scripts={"classpath:....-script.sql"})
    public void saveOneTest(){
    ....
    }

其他有:

  • Web side working with (@WebAppConfiguration) and either:
    • with @Parameters and @ClassRule + @Rule
    • without @Parameters and @ClassRule + @Rule

因此(在第二种方法下面):

@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
public class PersonaDeleteOneControllerTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    private ResultActions resultActions;

    ...

    @BeforeClass
    public static void setUp_(){
      ...
    }

    @Before
    public void setUp(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void deleteOneHtmlGetTest() throws Exception {

JUnit 实例化您的测试类。因此,Spring 不参与,因此无法将 AOP 建议应用于测试实例。

正如 Sergey Bespalov 所提到的,将 AspectJ 建议应用于测试实例的唯一方法是使用编译时或加载时编织。请注意,这不会在 Spring 中配置。 Spring 可用于为 Spring 管理的 bean 配置 AOP,但测试实例由测试框架(即您场景中的 JUnit 4)管理。

对于使用的测试Spring TestContext 框架不过,我不建议使用 AspectJ。相反,最好的解决方案是实施自定义TestExecutionListener执行日志记录。然后你可以注册TestExecutionListener明确地通过@TestExecutionListeners或者让它自动为您的整个套房拾取。对于后者,请参阅 Spring 参考手册的测试章节中有关自动发现的讨论。

Regards,

Sam (Spring TestContext 框架的作者)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring AOP - @Pointcut:@Test 方法的 @Before 建议不起作用 的相关文章

随机推荐

  • Maven 与查询 DSL 集成

    我正在尝试将 Maven 中的现有项目与查询 dsl 集成 我添加了如下依赖项
  • .Net Core 中的 PagedList.Core.Mvc PagedListPager Html 扩展不存在

    似乎是PagedList Core不包含 Html helper 的扩展方法 所以我无法使用下面的代码 Html PagedListPager Model page gt Url Action Index new page PagedLis
  • 加载实体时的 nhibernate 竞争条件

    我的网络应用程序中存在休眠竞争条件问题 我知道在使用旧版本的 log4net 时会发生这种情况 应该在 1 2 10 中修复 尽管我也经历过这种情况 因此 我们暂时禁用了 log4net 因为竞争条件会导致 IIS 崩溃 并且在生产中发生这
  • Bazel 的 Mingw-w64 工具链 (Ubuntu 20.04.1 )

    我正在尝试使用 Bazel 在 Ubuntu 上为 mingw w64 创建工具链 我正在关注这个教程 https docs bazel build versions master tutorial cc toolchain config
  • 按 Enter 键后调用 onChange 事件

    我是 Bootstrap 的新手 并遇到了这个问题 我有一个输入字段 只要我输入一位数字 函数就会onChange被调用 但我希望当我输入整个号码后按 Enter 时调用它 验证函数也有同样的问题 它调用得太快 var inputProce
  • 在表格视图中组合静态和原型内容

    有没有办法使用故事板将静态表格视图单元格 静态内容 与动态表格视图单元格 原型内容 结合起来 我建议您将表格视为动态表格 但在顶部包含您始终想要的单元格 在故事板中 放置一个UITableViewController并让它使用动态表 添加任
  • 使用 Robolectric 和 Mockito 测试 CursorLoader

    鉴于我正在开发一个简单的 ListFragment 在本例中 它从 MediaStore 读取艺术家列表 但稍后也会从不同的源读取数据 如下所示 EFragment public class ArtistsFragment extends
  • 如何将 SQL 查询返回值绑定到 psql 变量?

    背景 我正在写我的第一篇文章pgTAP http pgtap orgPL pgSQL 函数的测试用例 并从小处开始使用 psql 测试脚本 没有问题 但我遇到了一个小烦恼psql变量 http www postgresql org docs
  • ARM 的加载和存储字节和半字指令如何工作?签名与未签名有何作用?

    我刚刚开始学习 ARM 但无法理解加载和存储指令的确切作用 加载说明 ldrsb ldrb ldrsh ldrh ldr 店铺说明 strb strh str 有符号或无符号 加载半字 是什么意思 或者 加载字节 有符号或无符号 有符号和无
  • 2018 年底起的目标 API 级别要求

    我不知道这是否是询问以下问题的正确地方 我在 google play 开发者控制台上读到 2018 年下半年 Play 将要求新应用程序和应用程序 更新针对最新的 Android API 级别 这将需要 2018 年 8 月推出新应用程序
  • 使用 JPA 进行更新时如何排除实体字段

    有没有办法使字段在更新操作时不持久 但在使用 JPA Hibernate 4 创建操作时持久 我用这种方式尝试过 Transient Id Column name USER NAME nullable false length 75 pri
  • 在用户控件中处理画笔的更好方法是什么

    在 Paint 事件中使用新画笔是否是更好的方法 即 protected override void OnPaint PaintEventArgs e e Graphics SmoothingMode SmoothingMode AntiA
  • 替换整行会导致替换文本重复出现

    简单的问题 为什么 x replace y 产生 yy x replace y 相当于 Regex replace x y 结果yy您看到的内容基于其工作原理 根据 MSDN 在指定的输入字符串中 替换所有字符串匹配一个 具有指定替换字符串
  • 与平台无关的文件锁定?

    我正在进行一项计算量非常大的科学工作 时不时地会得出结果 这项工作基本上就是多次模拟同一件事 因此它被分配到使用不同操作系统的多台计算机上 我想将所有这些实例的输出定向到同一个文件 因为所有计算机都可以通过 NFS Samba 查看相同的文
  • 在 R 中使用 fromJSON 时处理 NaN

    我正在尝试使用 R 中的 fromJSON 函数来读取给我的 JSON 文件 但是该文件有NaN在其中 我无法正确读取它 这是我得到的错误 Error in feed push parser buf lexical error invali
  • 支持 19 位 Visa 和 Discover 卡验证的正则表达式模式 [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 目前我正在使用以下正则表达式 但它们不支持 Visa 和 Discover 的 19 位卡 请帮忙 visaCardPattern 4 0
  • 如何从python 3中的url读取html

    我查看了以前的类似问题 却变得更加困惑 在 python 3 4 中 我想根据给定的 url 将 html 页面作为字符串读取 在 Perl 中 我使用 LWP Simple 使用 get 来完成此操作 matplotlib 1 3 1 示
  • 如何在 Swift 中清空一个对象

    如何在 Swift 中将 nil 赋给对象 如果直接分配 我会收到错误 来自苹果文档 https developer apple com library ios documentation Swift Conceptual Swift Pr
  • Python:在 voronoi 图上绘制 scipy 图

    我正在尝试在 scipy 图之上进行绘图 使用 此解决方案 Python 在 scipy 图之上绘图 沃罗诺伊 https stackoverflow com questions 45997002 python plot on top of
  • Spring AOP - @Pointcut:@Test 方法的 @Before 建议不起作用

    我正在与 Spring框架4 3 2 方面J 1 8 9 JUnit Gradle 该项目基于多模块 In src main java main 我有一些 Aspect课程和他们按照预期的方式工作 我可以通过运行时和测试来确认 现在我需要