Spring@事务性和回滚不起作用,Spring集成测试



我在spring中进行集成测试,在本例中我测试了一个服务层。

我遇到了一个问题,在服务中的添加测试期间,回滚不起作用,并且总是向基础中添加一个项目,但不删除它。

我在测试类上放了注解@Transactional和@TestPropertySource,也有application-test.properties,并且测试成功,但是回滚没有执行,并且总是向测试数据库中添加一个新项。

我的测试类和添加地址的测试方法(最后一个(:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TicketServiceApplication.class)
@Transactional
@TestPropertySource("classpath:application-test.properties")
public class AddressServiceIntegrationTest {

@Autowired
private AddressService addressService;
@Autowired
private AddressRepository addressRepository;
@Test
public void findAllSuccessTest(){
    List<Address> result = addressService.finfAllAddress();
    assertNotNull(result);
    assertFalse(result.isEmpty());
    assertEquals(2, result.size());
}
@Test
public void findOneAddressExistTest_thenReturnAddress(){
    Long id = AddressConst.VALID_ID_ADDRESS;
    Address a = addressService.findOneAddress(id);
    assertEquals(id, a.getId());
}
@Test(expected = EntityNotFoundException.class)
public void findOneAddressNotExistTest_thenThrowException(){
    Long id = AddressConst.NOT_VALID_ID_ADDRESS;
    Address a = addressService.findOneAddress(id);
}
@Test
public void addAddressSuccessTest(){
    int sizeBeforeAdd = addressRepository.findAll().size();
    Address address = AddressConst.newAddressToAdd();
    Address result = addressService.addAddress(address);
    int sizeAfterAdd  = addressRepository.findAll().size();
    assertNotNull(result);
    assertEquals(sizeBeforeAdd+1, sizeAfterAdd);
    assertEquals(address.getCity(), result.getCity());
    assertEquals(address.getState(), result.getState());
    assertEquals(address.getNumber(), result.getNumber());
    assertEquals(address.getLatitude(), result.getLatitude());
    assertEquals(address.getLongitude(), result.getLongitude());
    assertEquals(address.getStreet(), result.getStreet());
}
}

我的应用程序测试属性:

spring.datasource.url= jdbc:mysql://localhost:3306/kts_test&useSSL=false&
useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.show-sql = true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

当执行添加地址测试时,每次在我的kts_test数据库(用于测试的数据库(中都会添加新项,而不是回滚。

这是控制台中的一个日志,您可以在其中看到回滚被调用但没有执行,因为当我在测试后刷新数据库时,留下了新项目,它没有被删除。

INFO 10216 --- [           main] o.s.t.c.transaction.TransactionContext   : Rolled back transaction for test: [DefaultTestContext@3e58a80e testClass = AddressServiceIntegrationTest, testInstance = com.ftn.services.address.AddressServiceIntegrationTest@4678ec43, testMethod = addAddressSuccessTest@AddressServiceIntegrationTest, testException = [null],...

最后,我尝试了@Transactional以上的方法,我也尝试了@Rollback以上的方法或@Rollback(true(,尝试更改application-test.properties,但我不再知道可能是什么错误。

如果有人能帮忙,我将不胜感激。非常感谢。

Hibernate默认使用MyISAM存储引擎创建表-该引擎不支持事务。您需要有InnoDB表:

  • 使用像Flyway这样的工具手动管理数据库迁移(imho首选选项,以便您可以完全控制(,然后关闭在测试中重新创建数据库
  • 或者设置正确的发动机配置:
spring.jpa.properties.hibernate.dialect.storage_engine=innodb

OR(已弃用(

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

最新更新