Verify a Key exists in a Hashmap
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
Reference: https://docs.oracle.com/
How to use it?
Supposedly, in an already existing method, let's say of creating a user profile we have 3 parameters of Name, role, and Company Name. Also, this method is being used at numerous places with different values. In an upcoming sprint, a request comes to add a new field as SSN Number, but only when a Setting for it in Admin Dashboard is enabled. So, to incorporate this case here either we will create a new function and replace it with the older one or make modifications to the existing one. To make changes to existing ones we at least need to add new parameters. So you now need to make sure that older test cases run as it is without the impact of new changes and also make the new changes work in newer test cases.
So F.R.I.E.N.D.S here we will make use of the HashMap concept. Just follow the below code snippet to understand it.
We have firstly created a simple Hashmap, in a real-life case, it could be a globally declared static object. Using this object we would verify a condition if the new step has to be done or not in the same old method.
Here above we have simply verified whether a hashmap key exists with the given name or not. So how you will use this is, in the test annotation(@Test) of your new test case you can put key-value pair values in the same hashmap object. Next in the method, you will verify that whether the same object with a key name is present, if it is present you can perform the required action under the If clause. If not, then using the else option you can simply print the message to the user in Report and move ahead.
Now you might be thinking about the impact the older test cases will face. So the answer is NO There will be NO impact. Since the method is every time looking for a KEY, if the KEY is present in the context of text annotation then it will perform the action and if it is not found then there is no change to older functionality.
You can simply copy the code in the above snippet and use it by molding it somewhat. Though the concept of identifying a key remains the same.
Comment/Mail/Message me on any of my platforms mentioned in the About Me section for a discussion over it.
Thanks
Comments
Post a Comment