Spring@JpaDataTest无事务
我正在尝试使用@JpaDataTest来测试我的存储库 我会像这样使用它:Spring@JpaDataTest无事务,spring,spring-test,Spring,Spring Test,我正在尝试使用@JpaDataTest来测试我的存储库 我会像这样使用它: @RunWith(SpringRunner.class) @DataJpaTest public class MyTest { @Autowired private TestEntityManager entityManager; @Autowired private MyRepo myRepo; @Test public void myTest() {
@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返回0实体
如果我删除注释以刷新
我犯了个错误
javax.persistence.TransactionRequiredException:没有正在进行的事务
我知道为什么
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如果我删除@ComponentScan,它会工作…嗨,Alexandre。你如何运行这个测试?Maven?嗨,我作为Junit测试运行,如果我使用Maven,它也会失败。昨天我把它和另一个班放在一起,但我没有保存它,一切都很好。。。