Careeroza
Real World Example
Reading Test Data from Text
Real projects keep test data outside the code, in CSV or Excel files. Here a text block stands in for a CSV file of login data. The program splits each line and decides the expected result, exactly like a data-driven test does before calling Selenium.
Example
Parse a CSV of login test data:
import java.util.*;
public class Main {
public static void main(String[] args) {
String csv = """
email,password,expected
[email protected],Test@123,success
[email protected],wrong,error
,Test@123,error
""";
List<String> lines = csv.lines().toList();
for (int i = 1; i < lines.size(); i++) { // skip header
String[] cols = lines.get(i).split(",", -1);
String email = cols[0].isEmpty() ? "(blank)" : cols[0];
System.out.printf("Row %d: email=%-15s password=%-9s expected=%s%n", i, email, cols[1], cols[2]);
}
System.out.println("Data rows: " + (lines.size() - 1));
}
}Output
Row 1: [email protected] password=Test@123 expected=success Row 2: [email protected] password=wrong expected=error Row 3: email=(blank) password=Test@123 expected=error Data rows: 3
Why This Matters
At banks and insurance companies, one login test often runs with 50 rows of data (valid users, locked users, expired passwords, special characters). The Java skills on this module's pages are exactly what you need to read and loop through that data.