Spring application is multitenant and use Atomikos as distributed transactions manager. Database servers are MySQl 5.7 ones.
I cannot reach my test data which I want to provide by @Sql
.
I even set @Transactional(isolation = READ_UNCOMMITTED)
as on test method and on service method under test as well. But I cannot see this data in test:
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));
}
}
Service under the test:
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);
}
....
I've checked if added row is visible after it was inserted by Spring test context.
It really is.
I set debug breakpoint in org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript
and execute select request
SELECT count(*) as c FROM materials_groups
and count with that connection is really +1.
But this additional row isn't visible for service under test:(