简体   繁体   English

在单元测试中带有spring-boot-starter-data-jpa的Spring Boot需要强制使用@DataJpaTest

[英]Spring Boot with spring-boot-starter-data-jpa in unit test needs mandatory @DataJpaTest

I'm testing Spring Boot capabilities as a newbie in this area. 我正在测试Spring Boot功能,作为该领域的新手。 I have a simple app with basic dependencies. 我有一个具有基本依赖性的简单应用程序。

  • sping-boot-starter-parent 1.5.7 sping-boot-starter-parent 1.5.7
  • sping-boot-starter sping-boot-starter
  • sping-boot-starter-data-jpa sping-boot-starter-data-jpa
  • sping-boot-starter-test sping-boot-starter-test

Then there is simple Application class with @SpringBootApplication annotation. 然后是带有@SpringBootApplication批注的简单Application类。 Then I have a simple DummyService with @Service annotation. 然后,我有一个带有@Service批注的简单DummyService。 Then I have created a simple test DummyServiceTest with one @Test method and @RunWith(SpringRunner.class) and @SpringBootTest annotations. 然后,我使用一个@Test方法以及@RunWith(SpringRunner.class)@SpringBootTest批注创建了一个简单的测试DummyServiceTest。

@SpringBootTest is the key problem. @SpringBootTest是关键问题。 With spring-boot-starter-data-jpa dependency and this annotation test requires even @DataJpaTest annotation. 具有spring-boot-starter-data-jpa依赖性,此注释测试甚至需要@DataJpaTest注释。 Without it the framework doesn't solve HibernateJpaAutoConfiguration or DataSource or other injection dependencies even if the test doesn't require using data. 没有它,即使测试不需要使用数据,该框架也无法解决HibernateJpaAutoConfigurationDataSource或其他注入依赖项。

Can I suppress it somehow? 我能以某种方式抑制它吗? I'm not any kind of Spring guru so my guess is that there is some simple configuration to handle this problem. 我不是任何一种Spring专家,所以我的猜测是可以通过一些简单的配置来解决此问题。

PS Ok, back on trees. PS好,回到树上。 Even with @DataJpaTest that test doesn't solve data dependencies. 即使使用@DataJpaTest ,该测试也不能解决数据依赖性。 I tried add @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) and it doesn't work either. 我尝试添加@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) ,它也不起作用。 I tried add @Transactional with the same result. 我尝试添加@Transactional具有相同的结果。 It's a little bit beyond ridiculous. 这有点荒谬。

If you aren't using JPA yet, then comment out / remove the dependency from build.gradle until you are using JPA. 如果尚未使用JPA,请注释掉/从build.gradle中删除依赖项,直到使用JPA。 You need to define a datasource and other configuration details before the Hibernate and/or JPA configuration will complete successfully. 您需要先定义数据源和其他配置详细信息,然后Hibernate和/或JPA配置才能成功完成。 Every application dependency gets resolved while @SpringApplicationConfiguration code is running, even if your current "hello world" test doesn't require JPA data. @SpringApplicationConfiguration代码运行时,即使您当前的“ hello world”测试不需要JPA数据,每个应用程序依存关系也会得到解决。

My current unit tests actually have @SpringBootTest commented out. 我当前的单元测试实际上已注释掉@SpringBootTest。 Here's a simplified view of how things are set up and working in my app's JPA related tests: 这是在我的应用程序的JPA相关测试中如何设置和工作的简化视图:

@RunWith(SpringJUnit4ClassRunner)
@SpringApplicationConfiguration(classes = DaemonApplication)
@ActiveProfiles('local')
//@SpringBootTest
@Transactional
public abstract class AbstractJpaTest extends AbstractTransactionalJUnit4SpringContextTests { 
    @BeforeTransaction
    public void setupData() throws Exception {
        deleteFromTables('User', 'User_Session', 'User_Handshake');
    }
}

and then 接着

class UserHandshakeRepositoryIntegrationTest extends AbstractJpaTest {

@Autowired UserHandshakeRepoImpl handshakeRepository;    


@Test
public void testSave() {
    UserHandshake handshake = handshakeRepository.save(new UserHandshake());
    assertThat(handshake.getId(), is(notNullValue()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导不添加 spring-boot-starter-data-jpa - Spring boot not adding spring-boot-starter-data-jpa Java:依赖项目中的spring-boot-starter-data-jpa依赖 - Java : spring-boot-starter-data-jpa dependency in a dependent project spring-boot-starter-data-jpa依赖错误 - spring-boot-starter-data-jpa dependency error 有关Spring-boot-starter-data-jpa的问题 - A question about the Spring-boot-starter-data-jpa spring-boot-starter-data-jpa:自动重新连接 - spring-boot-starter-data-jpa : Auto Reconnect H2 数据库是用 spring-boot-starter-data-jpa 创建的,但不是用 spring-boot-starter-data-jdbc 创建的 - H2 Database created with spring-boot-starter-data-jpa but not with spring-boot-starter-data-jdbc spring-data-jpa和spring-boot-starter-data-jpa之间的区别 - Difference between spring-data-jpa and spring-boot-starter-data-jpa 未找到依赖项 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.3' - Dependency 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.3' not found 排除 JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' 启动时导致 CRUDRepository 错误 - Exclude JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' causes error in CRUDRepository when starting 将“spring-boot-starter-data-jpa”依赖项添加到Spring项目时出错 - Error when adding “spring-boot-starter-data-jpa” dependency to Spring project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM