将 SpringBootTest 与 Spring 之前运行的其他测试一起使用时,如何确保 Eclipselink 发生加载时间编织

2024-05-08

我正在使用 Spring Rest Docs 为我的 REST 服务生成文档。这涉及运行单元(严格集成)测试,这些测试针对由测试启动的实时 Spring Boot 容器运行。测试类如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MySpringConfiguration.class)
@WebAppConfiguration
public class ApiDocumentation {

  private MockMvc mockMvc;

  @Rule
  public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");

  @Autowired
  private WebApplicationContext context;

  @Autowired
  private ObjectMapper objectMapper;

  @Before
  public void setUp() {
      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
            .apply(documentationConfiguration(this.restDocumentation))
            .build();
  }


  @Test
  public void testSomething() throws Exception {
  }
}

该应用程序使用 JPA 和 EclipseLink 来实现 EntityManager。

当我在 IDE 中独立运行测试时,或者作为使用 maven-surefire-plugin 运行 Maven 构建时唯一存在的测试时,一切正常。

然而,这并不是我想在套件中运行的唯一测试。当我在套件中运行其他测试时,我遇到了提到的问题here http://wiki.eclipse.org/EclipseLink/Examples/JPA/Spring#Common_Issues,即

“Spring的代理在应用程序访问Spring上下文之前不会初始化持久化上下文。如果应用程序在访问Spring上下文之前已经触发了持久化类的加载,则不会发生编织。”

并得到这样的错误:

异常描述:对象 [mypackage.MyEntity] 中未定义方法 [_persistence_set_someField_vh] 或 [_persistence_get_someField_vh]。

那么人们通常会做什么来解决这个问题呢?在不同的模块中运行 SpringBootTest 类来对访问实体进行单元测试?


就我所关心的动态编织引起的问题而言,如果将其设为静态,它应该可以正常工作。可能it https://stackoverflow.com/questions/28477122/static-weaving-eclipselink-jpa-with-spring-and-tomcat可以帮助你

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

将 SpringBootTest 与 Spring 之前运行的其他测试一起使用时,如何确保 Eclipselink 发生加载时间编织 的相关文章

随机推荐