Search This Blog

Breaking

Monday, 17 August 2020

How I implemented Optional class in my java API automation framework to avoid null pointer exception ?

 "I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object-oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years."

—Sir Tony Hoare (Known for Quicksort, Quickselect, Hoare logic, Null reference, Communicating Sequential Processes, Structured programming) Speaking at a software conference called QCon LondonAfter reading the statement from Sir Tony Hoare, You can understand the importance of null pointer reference. If you are a Java programmer, you probably know that It is one of the worst nightmares for us. It is a source of the myriad problems. In the series of my blog with Java8 and Automation, today, I will show the use of class Optional introduced in Java8 and how we used it in our automation script. We have some scenarios in our automation script, where we are creating one folder in the beginning and using the Folder key in the rest of our test cases to avoid creating multiple Folders.

Below is the code for that:


capturing the folder key from the already created folder

String folderKey =repositoryMap.get(RepositoryParameterEnum.FOLDERKEY.getValue());

The problem with this code is that, if there is no folder created, it was returning Null value. 

Which I was handling the script with below logic:

if (folderKey.equals(null)) {

 String folderName = "AutoFolder_" + Utils.getUniqueId();

BaseScript.createFolderServiceMethod(folderName);

}

folderKey = repositoryMap.get(RepositoryParameterEnum.FOLDERDKEY.getValue());

So this is one area where I found I can use Java8 optional class and revamp this code with better null pointer handling.

Optional Class in Java8: The concept of Optional is not new and other programming languages have similar constructs. For example - Scala has Optional[T], Haskell has Maybe type. So if you are familiar with these languages, you can correlate it. For rest, who are neophyte for this concept, let's understand it with a simple explanation:

Optional is a container type for a value that may be absent. I know, I know, you don’t like theory, so let's get into the code. So in our previous example, the folder key is the value, which sometimes holds a value and sometimes returns null. So I put it in optional, keeping in mind that the output may be null or maybe not.

String folderKey =repositoryMap.get(RepositoryParameterEnum.FOLDERKEY.getValue());

Optional<String> opt = Optional.ofNullable(folderKey);

Now, with optional I have many methods, which will help me to play this folder key value without much thinking about the null pointer. I used isPresent() method. isPresent() method returns true if the Optional contains a non-null value, otherwise, it returns false.

So, I changed my code:

if(opt.isPresent()) //returns true or will execute if the folderKey contains a non-null value

{

  FrameworkLogger.logStep("Folder key is " +folderKey);

  }

  else {

  String folderName = "AutoFolder_" + Utils.getUniqueId();

  FrameworkLogger.logStep("Create a new Folder");

 BaseScript.createFolderServiceMethod(folderName);;

  folderKey = repositoryMap.get(RepositoryParameterEnum.FOLDERDKEY.getValue());

  FrameworkLogger.logStep("Newly created Folder key is " +folderKey);

}

The code is more readable and handles the null value in a better way. So this is how, I implemented, another beautiful Java8 feature in our automation script. You can find a plethora of information about the Optional class on the Internet, so I am avoiding the part of creating the optional class and its methods. I hope, now when you handle, null value, you will use optional class and avoid the embarrassment of NullPointerException.

No comments:

Post a Comment