我有一个springboot应用程序,我在其中定义了一个实体,如下所示
@Entity
public class Organisation {
@Id
@GeneratedValue
@JsonIgnore
private Long id;
private String entityId;
private String type;
@OneToMany(mappedBy = "organisation", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<OrganisationProperty> properties = new HashSet<>();
public Organisation(String entityId, String type,Set<OrganisationProperty> properties) {
this.entityId = entityId;
this.type = type;
properties.forEach(p -> {
this.properties.add(new OrganisationProperty(p.getPropName(), p.getPropValue()));
});
}
在我的服务类中,我有一个删除属性的函数
public void editOrgType(String orgTypeName, List<OrgHierarchyEditProperty> editProperties) {
Set<OrganisationProperty> orgPropertiesToDelete = organisationPropertyRepository
.getOrganisationPropertyByTypeAndPropName(orgTypeName, editProperty.getOldPropName());
organisationPropertyRepository.deleteAll(orgPropertiesToDelete);
要获取的函数在repository类中定义
@Query("select p from Organisation o join OrganisationProperty p on o.id = p.organisation.id "
+ "where o.type = ?1 and p.propName = ?2")
public Set<OrganisationProperty> getOrganisationPropertyByTypeAndPropName(String type, String propName);
我正在通过一个junit测试用例测试相同的逻辑
@Before
public void setup() {
Set<OrganisationProperty> circProperties = new HashSet<>();
OrganisationProperty circProp = new OrganisationProperty("p", "v");
circProperties.add(circProp);
Organisation circle = new Organisation("circle1", "circle", circProperties);
circProp.setOrganisation(circle);
circle = organisationRepository.save(circle);
}
@Test
public void editOrgTypeAddDeleteTest() {
List<OrgHierarchyEditProperty> editProperties = new ArrayList<>();
OrgHierarchyEditProperty delProp = new OrgHierarchyEditProperty();
delProp.setEditMode(Mode.DELETE);
delProp.setOldPropName("p");
editProperties.add(delProp);
organisationService.editOrgType("circle", editProperties);
Organisation circle = organisationRepository.findByEntityId("circle1").get();
boolean deleted = circle.getProperties().stream().anyMatch(p -> p.getPropName().equals("p"));
assertFalse(deleted);
}
当我通过控制器调用服务时,逻辑工作正常,属性被删除。但是在junit测试用例中,函数organizationpropertyrepository.getorganizationpropertybytypeandpropname不返回任何内容,因此删除“p”的属性永远不会被获取和删除。如果我在服务中调试并调用OrganizationPropertyRepository.findall(),它会得到属性“p”,但不知何故,连接查询在junit中无法工作
暂无答案!
目前还没有任何答案,快来回答吧!