You track 'expected' and 'actual' in your head or a spreadsheet column.
Variables are those columns — named boxes holding the values your test compares.
A variable is a named container for a value. Java is statically typed: every variable declares what kind of value it holds, and the compiler refuses to mix them up. Annoying at first, lifesaving in big test suites — whole categories of bugs can't compile.
String — text: URLs, usernames, error messages. The type you'll use most.int — whole numbers: counts, quantities, status codes.double — decimals: prices, totals.boolean — true or false: is the element displayed? did the test pass?String expectedTitle = "Dashboard"; // declare type, name, value String actualTitle = "Dashbord"; // a bug! (typo) int cartItems = 3; double cartTotal = 149.97; boolean isLoggedIn = true; // comparison produces a boolean: boolean titleMatches = expectedTitle.equals(actualTitle); // false System.out.println("Title matches: " + titleMatches);
Title matches: false
For Strings, always compare with .equals(), never ==. == asks "are these the same object in memory?", while .equals() asks "do they contain the same text?" — which is what a tester means. Using == on Strings is the classic beginner bug, and it sometimes accidentally works, which makes it worse.
expected and actual variables meeting a comparison. This lesson is the anatomy of an assertion.Test code is read during failures, under pressure. String s1 tells the 2 a.m. debugger nothing; String expectedErrorMessage tells them everything. Java convention is camelCase: first word lowercase, each next word capitalized.
Fix: The value doesn't match the declared type — int attempts = "5"; stores text in a number box. Drop the quotes for numbers; keep them for Strings.
Fix: Almost always a typo or wrong capitalization — Java is case-sensitive, so expectedTitle and expectedTite are strangers. Read the symbol name in the error letter by letter.
Fix: Every statement ends with a semicolon. The error points at the line after the missing one as often as the line itself — check both.
In your head or on paper, declare variables (type, name, value) for a login test:
String loginUrl = "https://app.example.com/login"; String validUsername = "qa.tester"; String validPassword = "S3cure!Pass"; String expectedTitle = "Dashboard"; boolean rememberMe = false; int maxFailedAttempts = 5;