[英]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. 我有一个具有基本依赖性的简单应用程序。
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. 没有它,即使测试不需要使用数据,该框架也无法解决HibernateJpaAutoConfiguration
或DataSource
或其他注入依赖项。
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.