1

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。

但是这个额外的行对于被测服务是不可见的:(

4

0 回答 0