我正在使用 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 ,即
“Spring的代理在应用程序访问Spring上下文之前不会初始化持久化上下文。如果应用程序在访问Spring上下文之前已经触发了持久化类的加载,则不会发生编织。”
并得到如下错误:
异常描述:对象 [mypackage.MyEntity] 中未定义方法 [_persistence_set_someField_vh] 或 [_persistence_get_someField_vh]。
那么人们通常会做什么来解决这个问题呢?在不同的模块中运行 SpringBootTest 类来对访问实体进行单元测试?
最佳答案
就我所关心的动态编织引起的问题而言,如果将其设为静态,它应该可以正常工作。可能it可以帮助你
关于spring - 将 SpringBootTest 与 Spring 测试之前运行的其他测试一起使用时,如何确保 Eclipselink 发生加载时间编织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39891205/