Java Methods Cheat sheet for SDETs

Date:

Share post:

If you’re using Java for Test Automation, this cheat sheet will definitely be helpful.

Essential Java Methods for SDETs

CategoryMethodUsageExample
String HandlingString.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"
CollectionsList.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 HandlingFiles.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 & TimeLocalDate.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 & NumbersMath.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
AssertionsAssert.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 OperationsSystem.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 ExpressionsPattern.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
ThreadingThread.sleep()Pauses execution for a specified time.Thread.sleep(1000)
ExecutorService.submit()Submits a task for execution in a thread pool.executorService.submit(task)

QABash Nexus—Subscribe before It’s too late!

Monthly Drop- Unreleased resources, pro career moves, and community exclusives.

Ishan Dev Shukl
Ishan Dev Shukl
With 13+ years in SDET leadership, I drive quality and innovation through Test Strategies and Automation. I lead Testing Center of Excellence, ensuring high-quality products across Frontend, Backend, and App Testing. "Quality is in the details" defines my approach—creating seamless, impactful user experiences. I embrace challenges, learn from failure, and take risks to drive success.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Advertisement

Related articles

5 Vibe Testing Anti-Patterns That Destroy Your Pipeline (And How to Fix Them)

Why Anti-Patterns Undermine Your Vibe Testing Success Vibe testing—using AI-native frameworks to drive resilient, intent-based automation—promises reduced maintenance and...

Vibe Testing: How AI-Native Tools Are Rewriting Automation Rules

The New Era of Test Automation: From Vibe to AI-Native Self-Healing For years, “vibe testing” referred to rule-based automation...

Vibium AI: The $3.8 Billion Promise That Doesn’t Exist Yet—Why QA Teams Are Going Crazy Over Vaporware

The Most Anticipated Software Tool That You Can't Actually Use The testing world has gone absolutely insane over Vibium AI—Jason Huggins' promised...

Free MCP Course by Anthropic: Learn Model Context Protocol to Supercharge AI Integrations

Model Context Protocol (MCP): The Secret Sauce Behind Smarter AI Integrations If you’ve ever wished you could connect Claude...