If you’re using Java for Test Automation, this cheat sheet will definitely be helpful.
Essential Java Methods for SDETs
Category | Method | Usage | Example |
---|---|---|---|
String Handling | String.equals() | Compares two strings for equality. | "abc".equals("abc") // true |
String.contains() | Checks if a string contains a specific sequence of characters. | "automation".contains("auto") // true | |
String.substring() | Extracts a portion of a string. | "SDET".substring(1, 3) // "DE" | |
String.split() | Splits a string into an array based on a delimiter. | "A,B,C".split(",") // ["A", "B", "C"] | |
String.replace() | Replaces all occurrences of a character or substring. | "test".replace("t", "T") // "TesT" | |
String.trim() | Removes whitespace from both ends of a string. | " test ".trim() // "test" | |
String.toLowerCase() | Converts a string to lowercase. | "AUTOMATION".toLowerCase() // "automation" | |
String.toUpperCase() | Converts a string to uppercase. | "automation".toUpperCase() // "AUTOMATION" | |
String.isEmpty() | Checks if a string is empty. | "".isEmpty() // true | |
String.valueOf() | Converts other data types to a string. | String.valueOf(123) // "123" | |
Collections | List.add() | Adds an element to a list. | list.add("item") |
List.remove() | Removes an element from a list by index or value. | list.remove(0) | |
List.get() | Retrieves an element by index. | list.get(1) | |
List.size() | Returns the size of the list. | list.size() // 3 | |
Set.add() | Adds an element to a set (ignores duplicates). | set.add("item") | |
Set.contains() | Checks if a set contains an element. | set.contains("item") // true | |
Map.put() | Adds or updates a key-value pair in a map. | map.put("key", "value") | |
Map.get() | Retrieves a value by key from a map. | map.get("key") // "value" | |
Map.containsKey() | Checks if a map contains a specific key. | map.containsKey("key") // true | |
Map.keySet() | Returns a set of all keys in a map. | map.keySet() // ["key1", "key2"] | |
Collections.sort() | Sorts a list of elements. | Collections.sort(list) | |
Collections.reverse() | Reverses the order of a list. | Collections.reverse(list) | |
Arrays.asList() | Converts an array to a list. | Arrays.asList(1, 2, 3) // [1, 2, 3] | |
Arrays.sort() | Sorts an array of elements. | Arrays.sort(array) | |
File Handling | Files.readAllLines() | Reads all lines of a file into a list. | Files.readAllLines(Paths.get("file.txt")) |
Files.write() | Writes data to a file. | Files.write(Paths.get("file.txt"), "data".getBytes()) | |
File.exists() | Checks if a file exists. | new File("file.txt").exists() // true | |
File.mkdir() | Creates a new directory. | new File("dir").mkdir() | |
File.delete() | Deletes a file or directory. | new File("file.txt").delete() | |
BufferedReader.readLine() | Reads a single line from a file. | bufferedReader.readLine() | |
Date & Time | LocalDate.now() | Gets the current date. | LocalDate.now() // 2024-12-12 |
LocalTime.now() | Gets the current time. | LocalTime.now() // 14:30:00 | |
LocalDateTime.now() | Gets the current date and time. | LocalDateTime.now() // 2024-12-12T14:30:00 | |
DateTimeFormatter.ofPattern() | Formats a date or time in a specific pattern. | DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now()) // "2024-12-12" | |
Duration.between() | Calculates the duration between two time points. | Duration.between(start, end).toMinutes() // 10 | |
ChronoUnit.DAYS.between() | Calculates the number of days between two dates. | ChronoUnit.DAYS.between(date1, date2) // 5 | |
Math & Numbers | Math.max() | Returns the larger of two numbers. | Math.max(10, 20) // 20 |
Math.min() | Returns the smaller of two numbers. | Math.min(10, 20) // 10 | |
Math.pow() | Raises a number to the power of another. | Math.pow(2, 3) // 8.0 | |
Math.sqrt() | Returns the square root of a number. | Math.sqrt(16) // 4.0 | |
Integer.parseInt() | Converts a string to an integer. | Integer.parseInt("123") // 123 | |
Double.parseDouble() | Converts a string to a double. | Double.parseDouble("123.45") // 123.45 | |
Assertions | Assert.assertEquals() | Asserts that two values are equal (JUnit/TestNG). | Assert.assertEquals(actual, expected) |
Assert.assertTrue() | Asserts that a condition is true. | Assert.assertTrue(value > 0) | |
Assert.assertFalse() | Asserts that a condition is false. | Assert.assertFalse(value < 0) | |
Assert.assertNotNull() | Asserts that an object is not null. | Assert.assertNotNull(object) | |
System Operations | System.out.println() | Prints data to the console. | System.out.println("Hello, SDET!") |
System.currentTimeMillis() | Returns the current time in milliseconds. | System.currentTimeMillis() | |
System.getenv() | Retrieves environment variables. | System.getenv("PATH") | |
Regular Expressions | Pattern.compile() | Compiles a regular expression into a pattern. | Pattern.compile("[0-9]+") |
Matcher.find() | Checks if the pattern matches a substring. | matcher.find() // true | |
Matcher.group() | Retrieves matched groups from a regex pattern. | matcher.group() // "123" | |
String.matches() | Checks if a string matches a regex. | "12345".matches("\\d+") // true | |
Threading | Thread.sleep() | Pauses execution for a specified time. | Thread.sleep(1000) |
ExecutorService.submit() | Submits a task for execution in a thread pool. | executorService.submit(task) |
Subscribe to QABash Weekly ๐ฅ
Dominate โ Stay Ahead of 99% Testers!