Spring应用程序是多租户的,并使用Atomikos作为分布式事务管理器。 数据库服务器是MySQl 5.7。
我无法通过@Sql
找到我想要提供的测试数据。
我甚至将@Transactional(isolation = READ_UNCOMMITTED)
设置为测试方法和测试中的服务方法。但我无法在测试中看到这些数据:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
import static org.springframework.transaction.annotation.Isolation.READ_UNCOMMITTED;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MaterialGroupServiceIntegrationTests {
@Autowired
private MaterialGroupService materialGroupService;
private static final String MATERIALGROUP_FOR_UPDATE_ID = "17d96645-e4f6-42b6-9831-3c39f9dd3fdf";
private static final String MATERIALGROUP_FOR_UPDATE_NAME = "TestMeForUpdate";
private static final String NEW_NAME = "test_group_updated";
@Test
@Transactional(isolation = READ_UNCOMMITTED)
@Sql(statements = "INSERT INTO materials_groups (id, type, name) VALUES ('" + MATERIALGROUP_FOR_UPDATE_ID + "', 'Plastic', '" + MATERIALGROUP_FOR_UPDATE_NAME + "')",
config = @SqlConfig(dataSource = XAConfig.MASTER_DATA_SOURCE_NAME))
public void updateMaterialTest() throws MaterialGroupNotFoundException {
final MaterialGroupDTO forUpdate = materialGroupService.get(MATERIALGROUP_FOR_UPDATE_ID);
assertThat(forUpdate.getName(), is(MATERIALGROUP_FOR_UPDATE_NAME));
forUpdate.setName(NEW_NAME);
MaterialGroupDTO updated = materialGroupService.update(forUpdate);
assertThat(updated.getName(), is(NEW_NAME));
}
}
测试下的服务:
import static org.springframework.transaction.annotation.Isolation.READ_UNCOMMITTED;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Service
public class MaterialGroupServiceImpl implements MaterialGroupService {
@Autowired
private MaterialGroupRepository materialGroupRepository;
...
@Override
@Transactional(isolation = READ_UNCOMMITTED)
public MaterialGroupDTO get(String id) throws MaterialGroupNotFoundException {
MaterialGroup materialGroup = materialGroupRepository.findOne(id);
if (materialGroup == null) {
throw new MaterialGroupNotFoundException("Can't find material group with id= " + id);
}
return materialGroupDTOConverter.fromModelToDTO(materialGroup);
}
....
在Spring测试上下文插入后,我检查了添加的行是否可见。
它确实是。
我在org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript
中设置调试断点
并执行选择请求
SELECT count(*)as c FROM materials_groups
并且使用该连接计数实际上是+1。
但是这个额外的行对于被测服务是不可见的:(