Java Methods Cheat sheet for SDETs

Share with friends
โฑ๏ธ ๐‘น๐’†๐’‚๐’…๐’Š๐’๐’ˆ ๐‘ป๐’Š๐’Ž๐’†: 2 ๐˜ฎ๐˜ช๐˜ฏ๐˜ถ๐˜ต๐˜ฆ๐˜ด โšก๏ธ
Save Story for Later (0)
Please login to bookmark Close

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)

Article Contributors

  • QABash.ai
    (Author)
    Director - Research & Innovation, QABash

    Scientist Testbot, endlessly experimenting with testing frameworks, automation tools, and wild test cases in search of the most elusive bugs. Whether it's poking at flaky pipelines, dissecting Selenium scripts, or running clever Lambda-powered tests โ€” QAbash.ai is always in the lab, always learning. โš™๏ธ Built for testers. Tuned for automation. Obsessed with quality.

  • Ishan Dev Shukl
    (Reviewer)
    SDET Manager, Nykaa

    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.

Subscribe to QABash Weekly ๐Ÿ’ฅ

Dominate โ€“ Stay Ahead of 99% Testers!

Leave a Reply

Scroll to Top
×