jeudi 4 février 2016

How to cover the branch if it is expecting for a null value in junit ?? How to cover the else part in the below example?

In junit I am trying to cover the else part which is expecting for the Null value but i am getting Null pointer exception can someone help me on this ??

public Promise postProvision(final InstancePostProvisionRequest request) { try {

        logger.info("Adding the Service Instance Properties to the Instance !!");

        //get all the properties from the service instance
        List<AssetProperty> routeProperties = request.getServiceInstance().getAssetProperties();
        List<Property> proptoaddtoinstance = new ArrayList<Property>();

        if (routeProperties != null) {
            for (AssetProperty prop : routeProperties) {
                Property apforInstance = new Property();
                apforInstance.setName(prop.getName());
                apforInstance.setValue(prop.getStringValue());
                proptoaddtoinstance.add(apforInstance);
            }

            InstanceResponse response = new InstanceResponse();
            for (Property prop : proptoaddtoinstance) {
                request.getInstance().getProperties().add(prop);

            }
            response.getModified().add(request.getInstance());
            response.setStatus(Status.COMPLETE);
            return Promise.pure(response);
        }
        else {
            logger.error("No Properties Recieved !!");
            InstanceResponse response = new InstanceResponse();
            response.setStatus(Status.FAILURE);
            return Promise.pure(response);
        }

    }
    catch (Exception ex) {
        logger.error("Exception while adding the properties to the Instance !!");
        logger.error(ex.getMessage());
        InstanceResponse response = new InstanceResponse();
        response.setStatus(Status.FAILURE);
        return Promise.pure(response);
    }
}

Aucun commentaire:

Enregistrer un commentaire