问题
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 NoSuchMethodException
s 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?
回答1:
I solved the problem by using a custom SpringApplicationRunListener
's contextPrepared()
to remove the problematic BeanFactoryPostProcessor
s from the ApplicationContext
before they got executed by Spring.
回答2:
We encountered the same problem in a web app built with Spring Boot 2.1.6 and EclipseLink 2.7.6.
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?
The solution was to set eclipselink.weaving
JPA property to false
in @SpringBootTest
, and to true
in regular operation.
Persistence config:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaProperties">
<props>
<prop key="eclipselink.weaving">${jpa.eclipselink.weaving}</prop>
</props>
</property>
<!-- ... -->
</bean>
Then in application.properties
:
jpa.eclipselink.weaving=true
And on JUnit test class:
@TestPropertySource(properties = "jpa.eclipselink.weaving=false")
来源:https://stackoverflow.com/questions/45378444/springboottest-interferes-with-eclipselink-dynamic-weaving