18 June 2013

TDD Exam Questions

Last year I ran a three day in-house training on unit-testing and TDD using Eclipse. I wanted the course to be as interactive as possible to keep the attendees engaged all the time. Besides my slides I carried out exercises like Jason Gorman's TDD from Hell and played short movies to keep the content varied. At the end of each chapter I showed a slide with a few questions about the topic just covered. These questions gave the audience a break to recall the content just studied and reflect on it before moving on. I searched for such questions but did not find any, so I had to come up with my own.

These are my sanity-check questions. They are not complicated or long-winded, just simple questions to verify if the content was understood. Some questions also served as a base for further discussion. The developers in the audience were exposed to TDD for the very first time, so I kept them simple. If you are a seasoned developer I am sure these questions will not challenge you at all.

Writing ExamsUnit Testing with JUnit
  • What is a test fixture?
  • Which assert is used to compare the values of objects?
  • What about assertTrue(true);?
  • What about assertTrue(a.equals(b));?
  • How to test for an expected exception?
  • What about System.out.println(...); in tests?
  • How to data-drive tests?
  • How to time-out tests?
  • How to test the contents of a private field? (This is a trap, the answer is not to test it but to change the design.)
  • Does full code coverage mean the code is fully tested?
Test Driven Development
  • What are the benefits of using TDD?
  • Is TDD primarily a testing technique?
  • When do you write tests? (This is a joke and the expected answer is "all the f*cking time" according to Bryan Liles' TATFT.)
  • What should you do when you cannot add another test?
  • If the code passes all the unit tests why should you still change (refactor) it?
  • Why not refactor on a red bar?
Mocking and Test Doubles
  • Why use stubs or mocks?
  • How are objects called that are passed around but never actually used?
  • How are objects called that can return fixed values in answer to calls made.
  • How are objects called that can verify the behaviour of the system under test, in addition to using asserts to verify its state.
  • What is used for testing state?
  • What is used for testing behaviour?
  • What is a spy?
ExamRefactoring
  • What is your favourite Refactoring?
  • How do we refactor? (This is a word play and the expected answer is mercilessly.)
  • Should Refactoring and adding new code be done at the same time?
  • During Refactoring, how long does your code not compile?
  • During Refactoring, when should we run the tests?
  • What Refactoring can be used when a method is too long, has duplicated code or Feature Envy?
  • What Refactoring can be applied to long parameter lists?
  • In Eclipse, what do the short-cuts Alt-Shift-R, Alt-Shift-L and Alt-Shift-M do?
Working with Legacy Code
  • What is Legacy Code?
  • How can you break dependencies?
  • What are Singletons and why are they evil?
  • Do you write them?
Using these questions, small exercises and short movies I kept the participants fully engaged for a whole day of TDD theory. It was awesome!

1 comment:

Tom said...

Hi Peter, i've written an answer to your article/questions. Check and correct them under http://www.nullpointer.at/2013/06/26/tdd-exam-question-answers/ if you want to.
Best wishes
Thomas