assert_
glossary ↓ downloads
track: java · selenium
suite 02 · java foundations

Values & variables

Java foundations12 min
as a manual tester

You track 'expected' and 'actual' in your head or a spreadsheet column.

in automation

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.

The types you'll actually use

Variables.java
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);
predict: what does this print? think it through, then reveal
output
Title matches: false

The .equals() trap — learn it now

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.

Tester's mental model: every assertion you'll ever write is just expected and actual variables meeting a comparison. This lesson is the anatomy of an assertion.

Naming matters more in tests

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.

⚠ when it breaks — the classics
The compiler refuses my types
java: incompatible types: String cannot be converted to int

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.

cannot find symbol
java: cannot find symbol  symbol: variable expectedTite

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.

';' expected
java: ';' expected

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.

⚡ exercise · model a login test's data

In your head or on paper, declare variables (type, name, value) for a login test:

  1. The URL of the login page
  2. A valid username and password
  3. The expected page title after login
  4. Whether "remember me" should be checked
  5. The number of allowed failed attempts before lockout
show a solution
String  loginUrl          = "https://app.example.com/login";
String  validUsername     = "qa.tester";
String  validPassword     = "S3cure!Pass";
String  expectedTitle     = "Dashboard";
boolean rememberMe        = false;
int     maxFailedAttempts = 5;
key takeaways
  • Variables are typed, named containers; the compiler enforces the types.
  • String, int, double, boolean cover most test code.
  • Compare Strings with .equals(), never == .
  • Descriptive names are a kindness to whoever debugs the failure.
← Back to roadmap Conditionals & loops →