1

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.

Ruchi
  • 41
  • 6
  • `StringUtils.isBlank()` does not return `false` for `null` or empty strings, see http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-. Please [edit] your question to include a [mcve], which shows the return value of `StringUtils.isBlank()` and the argument given for that method call. – Progman Apr 12 '20 at 16:16
  • In my example , I am passing two values : "" and " " . And as, I'm not passing value for PUSH_TOKEN that means, it is null or may be empty string. And then, I also printed these values after getting from json and all are what these should be. And also printed values from StringUtils.isBlank(passing the values) and it gives false. – Ruchi Apr 13 '20 at 04:57
  • Please [edit] your question to include individual `System.out.println()` calls for debugging purposes for the three variables `deviceId`, `pushToken` and `appId`. Also print the return value of the `length()` method to see the actual lengths of these strings. Then print the individual results of `StringUtils.isBlank()` for these strings. Include the output you get with these debug lines to your question. If that doesn't help, provide a [mcve] which we can test. – Progman Apr 13 '20 at 07:15

0 Answers0