@DataJpaTest 数据库无数据
先看官方注释:
/** * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} * for a typical JPA test. Can be used when a test focuses <strong>only</strong> on JPA * components. * <p> * Using this annotation will disable full auto-configuration and instead apply only * configuration relevant to JPA tests. * <p> * By default, tests annotated with {@code @DataJpaTest} are transactional and roll back * at the end of each test. They also use an embedded in-memory database (replacing any * explicit or usually auto-configured DataSource). The * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation can be used to * override these settings. * <p> * If you are looking to load your full application configuration, but use an embedded * database, you should consider {@link SpringBootTest @SpringBootTest} combined with * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} rather than this * annotation. */原因:默认使用内存数据库,且case具有事务性,会自动回滚插入数据。如果想使用自己配置的数据库,不要使用这个注解。
解决:@DataJpaTest替换为下列注解。
@SpringBootTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)