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 リクエストを実行します
SELECT count(*) as c FROM materials_groups
その接続でカウントすると、実際には +1 になります。
ただし、この追加の行は、テスト中のサービスでは表示されません:(