Spring @JpaDataTest 无事务
Spring @JpaDataTest no transaction
我正在尝试使用@JpaDataTest 来测试我的存储库
我是这样使用它的:
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MyRepo myRepo;
@Test
public void myTest() {
assertEquals(0, myRepo.findAll().size());
entityManager.persist(new MyEntity());
//entityManager.flush();
assertEquals(1, myRepo.findAll().size());
}
}
测试未通过,因为第二个 findAll return 0 个实体
如果我删除评论以刷新
我遇到了一个错误
javax.persistence.TransactionRequiredException: no transaction is in progress
我找到原因了...
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如果我删除 @ComponentScan 它会起作用...
我正在尝试使用@JpaDataTest 来测试我的存储库
我是这样使用它的:
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MyRepo myRepo;
@Test
public void myTest() {
assertEquals(0, myRepo.findAll().size());
entityManager.persist(new MyEntity());
//entityManager.flush();
assertEquals(1, myRepo.findAll().size());
}
}
测试未通过,因为第二个 findAll return 0 个实体
如果我删除评论以刷新
我遇到了一个错误
javax.persistence.TransactionRequiredException: no transaction is in progress
我找到原因了...
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如果我删除 @ComponentScan 它会起作用...