This commit related to the article BAEL-6877 (#14676)
This commit aims to add a new test class to convert a HashMap string into a HashMap object using toString().
This commit is contained in:
		
							parent
							
								
									dcf2685179
								
							
						
					
					
						commit
						c4c613b5a8
					
				| @ -0,0 +1,42 @@ | ||||
| package com.baeldung.map; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| public class UsingtoStringUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void given_HashMapString_whenUsingtoString_thenConvertToHashMapObject() { | ||||
|         // Example string representation of a HashMap | ||||
|         String hashMapString = "{key1=value1, key2=value2, key3=value3}"; | ||||
| 
 | ||||
|         // Remove unnecessary characters | ||||
|         String keyValuePairs = hashMapString.replaceAll("[{}\\s]", ""); | ||||
| 
 | ||||
|         // Split into individual key-value pairs | ||||
|         String[] pairs = keyValuePairs.split(","); | ||||
| 
 | ||||
|         // Create a new HashMap | ||||
|         HashMap<String, String> expectedHashMap = new HashMap<>(); | ||||
|         expectedHashMap.put("key1", "value1"); | ||||
|         expectedHashMap.put("key2", "value2"); | ||||
|         expectedHashMap.put("key3", "value3"); | ||||
| 
 | ||||
|         HashMap<String, String> actualHashMap = new HashMap<>(); | ||||
| 
 | ||||
|         // Parse and populate the HashMap | ||||
|         for (String pair : pairs) { | ||||
|             String[] keyValue = pair.split("="); | ||||
|             if (keyValue.length == 2) { | ||||
|                 actualHashMap.put(keyValue[0], keyValue[1]); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // Assert that the converted HashMap matches the expected one | ||||
|         assertEquals(expectedHashMap, actualHashMap); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user