Java 如何使我的sql数据可用于Junit中的所有新事务?
我正在尝试创建一个Junit测试用例。我有一个create方法,它是从我的processor方法调用的,该方法在新事务中创建并保存实体。 问题是我在sql中加载的数据不可用于新事务 处理器方法:Java 如何使我的sql数据可用于Junit中的所有新事务?,java,spring,jpa,junit,Java,Spring,Jpa,Junit,我正在尝试创建一个Junit测试用例。我有一个create方法,它是从我的processor方法调用的,该方法在新事务中创建并保存实体。 问题是我在sql中加载的数据不可用于新事务 处理器方法: public void updateFoo(final Test test) throws ProcessingException { //Business logics Foo foo = this.fooHelper.createAndSaveFoo(test);
public void updateFoo(final Test test) throws ProcessingException {
//Business logics
Foo foo = this.fooHelper.createAndSaveFoo(test);
//other business logics
foo.setName ("Name");
...
...
}
助手方法:
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Foo createAndSaveFoo(final Test test) throws NotFoundException, BadRequestException {
if (this.isFooPresent(test)) {
throw new BadRequestException(LOGGER, "CANNOT_CREATE_FOO", "Foo already created for the test");
}
final Foo foo = this.fooDAO.save(this.fooFactory.createFoo(test));
LOGGER.debug("Foo is saved successfully!, Entity@@ {}", foo);
return foo;
}
工厂方法:
public Foo createFoo(final Test test) throws NotFoundException {
return Foo.FooBuilder.aFoo().
withId(test.getId()).
withDate((test.getDate()).
withSize(test.getSize()).
withType(getType(test.getTypeId()).
build();
}
private Type getType(final int id) throws NotFoundException {
return this.typeDAO.findById(id).orElseThrow(() -> new NotFoundException(LOGGER, TYPE_NOT_FOUND, "Invalid type id"));
}
实体:
@Entity
@Table(name = "foo")
public class Foo {
@Column(name = "id")
private Long id;
@Column(name = "date")
private Date date;
@Column(name = "size")
private Long size;
@ManyToOne
@JoinColumn(name = "type", foreignKey = @ForeignKey(name = "fk__foo__type_id"))
private Type type;
//getters, setters and builders
}
@Entity
@Table(name = "type")
public class Type {
@Column(name = "id")
private Long id;
@Column(name = "date")
private String name;
//getters and setters
}
试验方法:
@Test
@Transactional
@Sql(value = {"classpath:sql/create_foo_data.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(value = {"classpath:sql/clean_everything.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void foo_test() throws ProcessingException {
//GIVEN
Test test = new Test();
test.setId(12);
test.setDate(new Date());
test.setSize(20);
test.setTypeId(1);
// WHEN
this.fooProcessor.updateFoo(test);
// THEN
final List<Foo> foo = this.fooDAO.findAll();
assertEquals(1, foo.size());
assertEquals(20, foo.get(0).getSize());
assertEquals("Test2", foo.get(0).getType().getName());
}
日志:
尽管我在sql中插入了类型,但由于createAndSaveFoo()创建了新事务,因此不会检索该类型。
如何使我的sql数据可用于新事务?您是否尝试过在
@sql
注释中将transactionMode
显式设置为ISOLATED
?这可能有助于确保在执行测试方法之前提交插入。即
@Sql(value = {"classpath:sql/create_foo_data.sql"},
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED))
com.deep.common.core.exception.NotFoundException: errorCode: 'TYPE_NOT_FOUND' - errorMessage : 'Invalid type id'
at com.deep.process.factory.entity.FooFactory.lambda$createFoo$0(FooFactory.java:55)
at java.util.Optional.orElseThrow(Optional.java:290)
@Sql(value = {"classpath:sql/create_foo_data.sql"},
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED))