考虑一个例子,我有两个实体如下
属性.java
@Entity @Table(name = "attribute") public class Attribute { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Getter private Long id; <pre><code> @Column(name="name") @Getter private String name; @Column(name = "source") @Getter private String source; protected Attribute(){} public Attribute(final String name, final String source) { this.name = name; this.source = source; } </code></pre> }
AttributeGroup.java
@Entity @Table(name = "attribute_group") public class AttributeGroup { <pre><code> @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Getter private Long id; @Column(name="name") @Getter private String name; @Column(name = "value") @Getter private String value; @Getter @Column(name="attribute_id", nullable=false) protected Long attributeId; @Getter @OneToOne(optional=true, fetch = FetchType.EAGER) @JoinColumn(name="attribute_id", updatable=false, insertable=false, referencedColumnName="id") private Attribute attribute; protected AttributeGroup(){} } </code></pre>
两个存储库
AttributeRepository.java
公共(public)接口(interface)AttributeRepository{}
AttributeGroupRepository.java
public interface AttributeGroupRepository { <pre><code>/** * find list of Groups by attribute name * @param attribute * @return */ List<AttributeGroup> findByAttribute(Attribute attribute); </code></pre> }
AttributeGroupRepositoryTest.java
<pre><code>@RunWith(SpringRunner.class)
@DataJpaTest
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class})
@DatabaseSetup(AttributeGroupRepositoryTest.DATASET)
@DatabaseTearDown(type = DatabaseOperation.CLEAN_INSERT, value = { AttributeGroupRepositoryTest.DATASET })
public class AttributeGroupRepositoryTest {
protected static final String DATASET = "classpath:/attribute-group-test-data.xml";
@Autowired
private AttributeGroupRepository groupRepository;
@Test
public void findByAttributes(){
Attribute attribute=new Attribute("Data","abc");
List<AttributeGroup> groups = groupRepository.findByAttribute(attribute);
assertThat(groups.isEmpty(), Matchers.is(false));
assertThat(groups.size(), Matchers.equalTo(2));
assertThat(groups.stream().findFirst().get().getId(), Matchers.equalTo(5L));
assertThat(groups.stream().findFirst().get().getName(), Matchers.equalTo("GROUP1"));
assertThat(groups.stream().findFirst().get().getValue(), Matchers.equalTo("HW"));
assertThat(groups.stream().findFirst().get().getAttribute().getId(), Matchers.equalTo(3L));
assertThat(groups.stream().findFirst().get().getAttribute().getName(), Matchers.equalTo("Data"));
assertThat(groups.stream().findFirst().get().getAttribute().getSource(), Matchers.equalTo("abc"));
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<lima_attribute id="1" name="Issuer Ultimate Parent Name" source="VENTURE"/>
<lima_attribute id="2" name="Currency" source="abc"/>
<lima_attribute id="3" name="Data" source="abc"/>
<lima_attribute_group id="1" name="CurrencyGroup" value="AUS" attribute_id="2"/>
<lima_attribute_group id="2" name="CurrencyGroup" value="GBP" attribute_id="2"/>
<lima_attribute_group id="3" name="CurrencyGroup" value="BHD" attribute_id="2"/>
<lima_attribute_group id="4" name="CurrencyGroup" value="AFA" attribute_id="2"/>
<lima_attribute_group id="5" name="GROUP1" value="HW" attribute_id="3"/>
<lima_attribute_group id="6" name="GROUP1" value="VOL" attribute_id="3"/>
</dataset>
上面的测试用例抛出异常
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.entity.attribute.Attribute; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.entity.attribute.Attribute
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.entity.attribute.Attribute
由于此测试依赖于属性存储库,并且 Junit 是单元测试用例,因此我不想使用属性存储库。
还有其他方法可以模拟其他存储库数据吗?
最佳答案
我找到了无需使用其他存储库即可执行测试用例的方法。
替换下面的行:
List<AttributeGroup> groups = groupRepository.findByAttribute(attribute);
与
List<AttributeGroup> groups = groupRepository.findByAttributeNameAndAttributeSource("Data","abc");
关于java - 编写 JPA 存储库 JUnit,其中一个存储库依赖于其他存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48636905/