我如何在一个 MVC 应用程序中组合 @DataJpaTest @SpringBootTest 来测试每一层?

How can I combine @DataJpaTest @SpringBootTest in one MVC application for testing every layer?

这里很好的解释了为什么@DataJpaTest @SpringBootTest不应该混在一个应用中。

但几乎没有解释这种情况,无论如何都需要测试 MVC SpringBoot 应用程序的每一层(从我的角度来看,不仅要测试一层或另一层,还要测试两层甚至所有层,不是吗?)

因此有人提出了使用@AutoConfigureTestDatabase 而不是@DataJpaTest 的解决方案,但没有完成工作(((

所以我的问题是:使用@AutoConfigureTestDatabase 是合适的解决方案吗?如果是,请说明详情。如果不是,请提出更合适的解决方案,谢谢

假设您想要完整的应用程序,除了内存中的数据库,是的,将 @SpringBootTest 与 @AutoConfigureTestDatabase 结合使用是一个合适的解决方案,因为它就是这样做的。

也提到in the documentation of @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.

我尝试使用 @AutoConfigureTestDatabase 东西,但它没有用,然后,我发现这个 并添加 @Transactional 有效!

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
@Transactional
public class ClippingControllerTest {

相关问题:

  1. How to test Spring CrudRepository using @DataJpaTest in Spring boot 2.1.0.M4 using Junit 5
  2. Error while testing: found multiple declaration of @BootstrapWith for test class
  3. Database cleanup after Junit tests

相关文章:

  1. https://brightinventions.pl/blog/clear-database-in-spring-boot-tests/
  2. https://medium.com/@dSebastien/cleaning-up-database-tables-after-each-integration-test-method-with-spring-boot-2-and-kotlin-7279abcdd5cc
  3. https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing