Top Banner
simple testable code Félix-Étienne Trépanier developer @wajam http://musicdrivendevelopment.com
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Simple testable code

simple testable code

Félix-Étienne Trépanierdeveloper @wajam

http://musicdrivendevelopment.com

Page 2: Simple testable code

_why?

maintainabilityand

evolution

Page 3: Simple testable code

ingredients not recipe

think    |    follow

Page 4: Simple testable code

coding process

understand---

apply

Page 5: Simple testable code

guard rails design and programming

even if tested, bad code kills bad unit test slows you down

Page 6: Simple testable code

what and how

TDD is very similar to getting things done with small children around. I just ask myself, "Now what was I trying to do?" every 5 minutes.

@carinmeier

Page 7: Simple testable code

production code

focus on...

Page 8: Simple testable code

core values

simplicity over flexibility

concreteness over abstraction

soft-coupled tests over full code coverage

test aware dev over test first dev

Page 9: Simple testable code

simple code

Aesthetic simplicity

• clean

• formatted

Cognitive simplicity

• clear concepts

• logically organized

• No unecessaryindirection

• small state space

Page 10: Simple testable code

clean code

no duplicate codeprecise and explicit

consistentsingle purpose artifacts

Page 11: Simple testable code

simple code

values are simpler than objectdata structures are simpler than classes

stateless is simpler than stateful

Page 12: Simple testable code

• Clean Code: A Handbook of Agile Software Craftsmanship, Robert C. Martin

• Simple Made Easy, Rich Hickey,  http://www.infoq.com/presentations/Simple-Made-Easy

• The Master, The Expert, The Programmer, Zed Shaw, http://zedshaw.com/essays/master_and_expert.html

references

Page 13: Simple testable code

by @alex_normand

refactoring can save you

Page 14: Simple testable code

test in isolation

speed

control

stability

Page 15: Simple testable code

all about dependencies

The key to write testable code is to inject some of the class dependencies via constructor, method parameters (or setter methods).

In the test code, test objects are passed to the class under test.

Page 16: Simple testable code
Page 17: Simple testable code

static dependencies

Example: MessageSender.java

The problem is not to depend on concrete classes, but to have static dependencies.

The new operator is static.

Page 18: Simple testable code

test code paradox

test code is strongly coupled to the production code

Page 19: Simple testable code

test code is code!

use test libraries

keep test code clean and simple

Page 20: Simple testable code

use the right test object

dynamic mock: Provided by unit test libraries such as Mockito. Simple to use and limit the amount of test code to a minimum.

production code: Add dependencies to the production code. Easier with behavior-less classes such as data holder.

hand coded mock: May be needed if behavior to mock is complex. Can be really painful to maintain.

Page 21: Simple testable code

assert behavior not implementation

• Testing implementation makes the test code fragile

• Verify return values and mandatory side effects

• Avoid asserting more than needed• One assert per test?

Page 22: Simple testable code

easy with EasyMock

Page 23: Simple testable code

assert only what is needed

Page 24: Simple testable code

stateless

Example: Statefull/StatelessProcessor.java

Stateless classes have no state space.

Stateless classes have do not worry about multithreading.

Stateless classes are simpler to setup for test.

Page 25: Simple testable code

summary

• aesthetic simplicity• cognitive simplicity• refactoring to keep code simple• inject dependencies• minimal test code• behavior validation