如何将@SpringBootTest(webEnvironment) 与@DataJpaTest 一起使用?
How can I use @SpringBootTest(webEnvironment) with @DataJpaTest?
我正在尝试测试 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
设置了:
有一些使用 @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
.
实际上,在 中回答时,它解决了眼前的问题,但是您将无法使用 REST 客户端进行任何涉及数据库的测试,因为 @Transactional
会阻止保存数据以供客户端获取。
要使其正常工作,不应使用 @Transactional
。相反,应该使用 DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD
。这大大减慢了每个测试(如每次测试 1 秒到 10 秒),但至少它有效。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = MyApp.class
)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@AutoConfigureTestDatabase
@AutoConfigureWebTestClient
public class MyResourceTest {
@Autowired
private TestRestTemplate restTemplate;
...
}
我正在尝试测试 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
设置了:
有一些使用 @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
.
实际上,在 @Transactional
会阻止保存数据以供客户端获取。
要使其正常工作,不应使用 @Transactional
。相反,应该使用 DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD
。这大大减慢了每个测试(如每次测试 1 秒到 10 秒),但至少它有效。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = MyApp.class
)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@AutoConfigureTestDatabase
@AutoConfigureWebTestClient
public class MyResourceTest {
@Autowired
private TestRestTemplate restTemplate;
...
}