Ruchi
Ruchi

Reputation: 41

Why org.apache.commons.lang.StringUtils.isBlank() returns false for empty and null values

I saw the implementation of StringUtils.isBlank() and was surprised that in my case, it is not working as expected.

I was writing a Junit and I faced this issue.

AmazonSNSRegistrationService.java:

public String doAWSSNSNDeviceRegistration(Map<String, Object> jsonBody){

    String deviceId = (String) jsonBody.get(PushNotificationsConstants.DEVICE_ID);
    String pushToken = (String) jsonBody.get(PushNotificationsConstants.PUSH_TOKEN);
    String appId = (String) jsonBody.get(PushNotificationsConstants.APP_ID);


    if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(pushToken) || StringUtils.isBlank(appId)) {
        System.out.println("$$$$$ Empty");
        throw new Exception("Required parameters are empty.");
    }
    return registerWithSNS(pushToken, deviceId, appId);

  }

AmazonSNSRegistrationServiceTest.java:

@Test
public void doAWSSNSNDeviceRegistrationBlankTest() {
    Map<String, Object> jsonBody = new HashMap<String, Object>();

    jsonBody.put(PushNotificationsConstants.APP_ID, "");
    jsonBody.put(PushNotificationsConstants.DEVICEID, " ");

    try{
        amazonSNSRegistrationService.doAWSSNSNDeviceRegistration(jsonBody);
    }
    catch(Exception e){

    }
}

I'm not passing Push Token , so it will be null. Other values are either "" or " ".

But in if condition, none of the statements give true. All return false and the Exception is not thrown. I want this code to throw exception when any of the statement in if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(pushToken) || StringUtils.isBlank(appId)) returns true.

Upvotes: 1

Views: 1372

Answers (0)

Related Questions