jpaspring-bootjuniteclipselinkload-time-weaving

@SpringBootTest interferes with EclipseLink dynamic weaving


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.

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.

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.

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?


Solution

  • 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.