Software Testing in Java

Why Automate Testing?

  1. Achieves stakeholder goals
  2. Meets functional requirments
  3. Correctly handles corner cases

automatedtesting

Testing Code
Writing Good Tests

Writing Test Code with JUnit & Java “JUnit is a Testing framework.”

This video is an introduction how to make a Junit test in eclipse.

Given the preconditions for the test, what should the code look like when the behavior happens…

GIVEN – WHEN – THEN

Failures VS. Errors

  • Failure
  • Tried to check a behavior
  • An assertion failed
  • Suggests the code is broken
  • Error
  • At any point in the test
  • An exception was thrown
  • Suggests the test is broken

Good Tests

Why should we care about test code quality?
Costs
– Maintenance
– Readability- Coupling

Tests are code, treat them like it… Test code is the same thing as application code if you want to be able to read your code after a year.

What does a good test look like?

DRY (Don’t repeat yourself)
Every piece of knowledge must have a single, unambiguous, authoritative representation within tests.
Behavior not implementation
Try to test the behavior of the class in action, don’t trans-pass the inner class to test it… (Don’t look at private methods)
– Implementation: “Exposing private state results in brittle and hard to maintain tests”
– Behavior: You can change the implementation and the tests still passes
Well named (For ex: PersonController the test should be PersonControllerTest)
– Naming provides executable documentation
– Maintenance: What test does what?
– Readability: When we are reading our test we need to tie our test to the body of the code
Diagnostics
for example: assertEquals(“Wrong quantity of coffee”, 1, order.size());