I am working on a spring boot application. I have developed a functionality that works well without error. However, when I did an integration test with the same code I have the following error:
partnerOrderHeaders assoc2s
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.Object1.assoc2s, could not initialize proxy - no Session
Here is the method in which there is the error
private List<ObjectClusterData> constructObjectClusterDataFromObject1(Object1 Object1) {
List<ObjectClusterData> ObjectClusterDatas = new ArrayList<ObjectClusterData>();
ObjectClusterData ObjectClusterData = new ObjectClusterData();
Set<Assoc1> Assoc1s = Object1.getPartnerOrderHeaders();
Object2 Object2 = null;
Set<Object2> Object2s = null;
ObjectItemClusterData ObjectItemClusterData = null;
for (Assoc1 Assoc1 : Assoc1s) {
ObjectClusterData = new ObjectClusterData();
Object2 = Assoc1.getHeader();
ObjectClusterData.setNum(Object2.getNum());
PartData partData = Object1 != null ? ConvertDomainObjectToDataObjectAndReverse.convertObject1ToPartData(Object1) : null;
ObjectClusterData.setPartDatas(Arrays.asList(new PartData[]{partData}));
Object2s = Object2.getItems();
List<ObjectItemClusterData> ObjectItemClusterDatas = new ArrayList<ObjectItemClusterData>();
for (Object2 Object2 : Object2s) {
ObjectItemClusterData = new ObjectItemClusterData();
ObjectItemClusterData.setProperty1(Object2.getProperty1());
ObjectItemClusterData.setProperty2(Object2.getProperty2());
ObjectItemClusterData.setProperty3(Object2.getProperty3());
ObjectItemClusterData.setDimData(Object2.getDim() != null ? ConvertDomainObjectToDataObjectAndReverse.convertDimObjectToDimObjectData(Object2.getDim()) : null);
ObjectItemClusterData.setMat(Object2.getMaterials() != null ? ConvertDomainObjectToDataObjectAndReverse.convertDimMatObjectToMatObjecData(Object2.getMat()) : null);
ObjectItemClusterDatas.add(ObjectItemClusterData);
}
ObjectClusterData.setObject2s(ObjectItemClusterDatas);
ObjectClusterDatas.add(ObjectClusterData);
}
return ObjectClusterDatas;
}
The error occured at this line
for (Assoc1 Assoc1 : Assoc1s){
But the important thing is that the functionality works without error while it does not work in the integration test part. I think that I miss a configuration set up in the test part. Could you help me please
As Nick asked me, I add test code
@Test
@WithMockUser(username = LOGIN, password = PASSWRD, roles = ROLE)
public void testGetOrderReferenceByOrderWithNoError() throws Exception {
// getInsertAndUpdateModificationInDateWindow
restCustomerOrderMockMvc
.perform(get("/api/orders/modification?"
+"beginInsertionOrModificationDate=" + ORDER_ZONE_DATE_TIME_NO_1+
"&endInsertionOrModificationDate=" + ORDER_ZONE_DATE_TIME_NO_4)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.resultCode").value(BusinessErrorCode.NO_ERROR.getCode()))
.andExpect(jsonPath("$.size").value("1"))
.andExpect(jsonPath("$.entities[0]." + F_OBJECT1_NUM).value(NUM_1))
.andExpect(jsonPath("$.entities[0].partDatas[0]" + F_USER_COM_API).value(PART_1))
.andExpect(jsonPath("$.entities[0].Object2s[0]" + F_ITEM_NO).value(ITEM_NO_1))
.andExpect(jsonPath("$.entities[0].Object2s[0].dimensionData." + F_WEIGHT.value(WEIGHT))
.andExpect(jsonPath("$.entities[0].Object2s[0].materialData." + F_GRADE).value(GRADE_1));
}