TransactionsTest (pass)

apache_commons_compress
Terekhin Alexander 5 years ago
parent a8b29ac936
commit 8a8ed24cfa
  1. 43
      src/test/java/me/bearns/fias/TransactionsTest.java
  2. 33
      src/test/java/me/bearns/fias/helper/TransactionalSaveHelper.java

@ -1,45 +1,57 @@
package me.bearns.fias;
import me.bearns.fias.domain.FiasVersion;
import me.bearns.fias.helper.TransactionalSaveHelper;
import me.bearns.fias.repository.FiasVersionRepository;
import org.junit.Before;
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.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static me.bearns.fias.helper.TransactionalSaveHelper.FIRST;
import static me.bearns.fias.helper.TransactionalSaveHelper.NEXT;
import static org.springframework.transaction.annotation.Isolation.READ_COMMITTED;
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(TransactionalSaveHelper.class)
public class TransactionsTest {
private static final long FIRST = 1L;
private static final long NEXT = 2L;
@Autowired
private FiasVersionRepository repository;
@Autowired
private TransactionalSaveHelper helper;
@Before
public void before(){
final FiasVersion version = new FiasVersion(FIRST, null, null, null);
repository.save(version);
try {
addNext();
} catch (Exception e) {
//just as planed
e.printStackTrace();
}
helper.setRepository(repository);
}
@Test
public void transactionTest(){
helper.addFirst();
try {
helper.addNext();
assert false;
} catch (Exception e) {
//e.printStackTrace();
//just as plained
assert true;
}
final List<FiasVersion> versions = repository.findAll();
versions.stream().map(FiasVersion::getVersionId).forEach(System.out::println);
@ -50,10 +62,5 @@ public class TransactionsTest {
}
@Transactional
public void addNext() throws Exception {
final FiasVersion nextVersion = new FiasVersion(NEXT, null, null, null);
repository.save(nextVersion);
throw new Exception("test");
}
}

@ -0,0 +1,33 @@
package me.bearns.fias.helper;
import me.bearns.fias.domain.FiasVersion;
import me.bearns.fias.exceptions.CommonException;
import me.bearns.fias.exceptions.UnmarshallingException;
import me.bearns.fias.repository.FiasVersionRepository;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.transaction.annotation.Transactional;
@TestComponent
public class TransactionalSaveHelper {
private FiasVersionRepository repository;
public static final long FIRST = 1L;
public static final long NEXT = 2L;
public void setRepository(FiasVersionRepository repository) {
this.repository = repository;
}
@Transactional(rollbackFor = Exception.class)
public void addNext() throws CommonException {
final FiasVersion nextVersion = new FiasVersion(NEXT, null, null, null);
repository.save(nextVersion);
throw new UnmarshallingException("test");
}
public void addFirst() {
final FiasVersion version = new FiasVersion(FIRST, null, null, null);
repository.save(version);
}
}
Loading…
Cancel
Save