我正在尝试测试 JAX-RS 应用程序,但我不想模拟数据,特别是因为现有 @DataJpaTest
有一个 buildData
方法
这是我到目前为止正在尝试的:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = MyApp.class
)
@DirtiesContext
@DataJpaTest
public class MyResourceTest {
我收到以下错误
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [app.MyResourceTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper)]
我看到的其他类似的没有谈论 webEnvironment
设置:
- How do I import configuration classes in a @DataJpaTest in a SpringBootTest?
- Recommended way to remove @DataJpaTest when used along with @SpringBootTest
有一些使用@AutoConfigureTestDatabase
的解决方案,但是当我这样做时,只有第一个有效,因为buildData
用@Before
注释(与 @DataJpaTest 相同)因为我希望数据在每次测试之前都是原始的,这样我就可以进行失败场景。
切换到 @BeforeClass
也不起作用,因为我将无法使用 @Autowire Repository
对象。
最佳答案
@DataJpaTest
文档说明如下:
If you are looking to load your full application configuration, but use an embedded database, you should consider
@SpringBootTest
combined with@AutoConfigureTestDatabase
rather than this annotation.
请记住 @DataJpaTest
注释为 @Transactional
和 @DirtiesContext
。因此,您可能需要这些注释以及 @AutoConfigureTestDatabase
.
关于java - 如何将 @SpringBootTest(webEnvironment) 与 @DataJpaTest 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57609818/