@SpringBootTest会干扰EclipseLink动态编织

编程入门 行业动态 更新时间:2024-10-26 12:22:47
本文介绍了@SpringBootTest会干扰EclipseLink动态编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的公司正在使用Spring Boot,Spring MVC,带有EclipseLink和动态编织的JPA开发Web应用程序。 我的任务是准备UI的实现和集成测试,使用JUnit和@SpringBootTest将应用程序分解,并使用Selenium与之交互。

My company is developing a web application using Spring Boot, Spring MVC, JPA with EclipseLink and dynamic weaving. My task is to prepare implementation of UI and integration tests spinning up the application using JUnit and @SpringBootTest and interacting with it using Selenium.

如 Spring Boot测试功能,使用 @SpringBootApplication 批注的测试可以利用 @MockBean 批注为bean定义Mockito模拟在ApplicationContext中。 这是通过注册 BeanFactoryPostProcessor , MockitoPostProcessor ,递归地扫描带有 @Component 或 @Configuration 用于用 @MockBean 注释的类和字段。

As stated at Spring Boot Testing Features, Tests using the @SpringBootApplication annotation can make use of the @MockBean annotation to define Mockito mocks for beans within the ApplicationContext. This is achieved by registering a BeanFactoryPostProcessor, MockitoPostProcessor, recursively scanning classes annotated with @Component or @Configuration for classes and fields annotated with @MockBean.

不幸的是,这导致这些类中引用的实体类在应该扫描它们的 LocalContainerEntityManagerFactoryBean 之前加载。实例化并使用 LoadTimeWeaver 进行设置,从而导致跳过那些实体的加载时间。 对于执行持久性操作时通过编织类似 _persistence_propertyChange()创建的方法,这会导致 NoSuchMethodException s。

Unfortunately, this causes the entity classes referenced in those classes to be loaded before the LocalContainerEntityManagerFactoryBean that is supposed to scan for them is instantiated and set up with a LoadTimeWeaver, thus causing the load time weaving for those entities to be skipped. This leads to NoSuchMethodExceptions for methods created by weaving like _persistence_propertyChange() when persistence actions are performed.

是否可以将 @SpringBootTest 与EclipseLink和动态编织一起使用? 如果没有,那么为最近的Spring Boot版本设置集成测试的一个很好的选择是什么?

Is it possible to use @SpringBootTest with EclipseLink and dynamic weaving? If not, what would be a good alternative to set up an integration test for a recent Spring Boot version?

推荐答案

我通过使用自定义 SpringApplicationRunListener 的 contextPrepared()解决了问题, code> ApplicationContext 中的code> BeanFactoryPostProcessor ,然后由Spring执行。

I solved the problem by using a custom SpringApplicationRunListener's contextPrepared() to remove the problematic BeanFactoryPostProcessors from the ApplicationContext before they got executed by Spring.

更多推荐

@SpringBootTest会干扰EclipseLink动态编织

本文发布于:2023-10-08 02:28:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:干扰   动态   SpringBootTest   EclipseLink

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!