FlowTableManager implements AutoCloseable { private UserIntentVnMapping getUserIntentVnMapping(UserId userId) { ReadOnlyTransaction readOnlyTransaction = dataProvider.newReadOnlyTransaction(); InstanceIdentifier<UserIntentVnMapping> userIntentVnMappingIid = InstanceIdentifier .builder(IntentVnMappingResults.class) .child(UserIntentVnMapping.class, new UserIntentVnMappingKey(userId)) .build(); Optional<UserIntentVnMapping> result = null; try { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, userIntentVnMappingIid).get(); } catch (Exception e) { LOG.error("Exception:",e); } if (result.isPresent()) { LOG.debug("getUserIntentVnMapping OK"); return result.get(); }else{ LOG.debug("getUserIntentVnMapping ERROR"); return null; } } FlowTableManager(DataBroker dataProvider,
                            PacketProcessingService packetProcessingService,
                            PhyConfigLoader phyConfigLoader); @Override void close();  }
@Test public void testGetUserIntentVnMapping()throws Exception{ UserId userId = mock(UserId.class); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); CheckedFuture checkedFuture = mock(CheckedFuture.class); Optional<UserIntentVnMapping> result = mock(Optional.class); UserIntentVnMapping vnMapping; Class<FlowTableManager> class1 = FlowTableManager.class; Method method = class1.getDeclaredMethod("getUserIntentVnMapping",new Class[]{UserId.class}); method.setAccessible(true); when(dataProvider.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture); when(checkedFuture.get()).thenReturn(result); when(result.isPresent()) .thenReturn(false) .thenReturn(true); when(result.get()).thenReturn(mock(UserIntentVnMapping.class)); vnMapping = (UserIntentVnMapping) method.invoke(flowTableManager,userId); Assert.assertTrue(vnMapping == null); vnMapping = (UserIntentVnMapping) method.invoke(flowTableManager,userId); Assert.assertTrue(vnMapping != null); }