Unit	testing on	Android:
why and how?
DevFest Bucharest
Danny	Preussler
@PreusslerBerlin
Untested	Code is	the	Dark	Matter	of	Software
'El	Gordo'	Galaxy	Cluster	Even	Bigger	Than	Thought	(NASA,	Chandra,	04/03/14)	by	NASA's	Marshall	Space	Flight	Center,	CC	by	2.0;	flickr.com/photos/nasamarshall/13901530984
it	makes	up	to 90%
of	the	software	universe
Untested	Code is	the	Dark	Matter	of	Software
'El	Gordo'	Galaxy	Cluster	Even	Bigger	Than	Thought	(NASA,	Chandra,	04/03/14)	by	NASA's	Marshall	Space	Flight	Center,	CC	by	2.0;	flickr.com/photos/nasamarshall/13901530984
How	do	we	test
our	apps	today?
http://martinfowler.com/bliki/TestPyramid.html
Manual
UI
Service
Unit
Champ!®	Mini	Swirls	Birthday	Party	Ice	Cream	Cones	by	Blue	Bunny®,	CC	by	2.0;	www.flickr.com/photos/bluebunnyicecream/5449137794
http://martinfowler.com/bliki/TestPyramid.html
The	problem of
functional	tests
Something is	broken!
@PreusslerBerlin
What	is	broken?
@PreusslerBerlin
Where	it’s	broken?
@PreusslerBerlin
It’s	end	to	end by	definition!
@PreusslerBerlin
Production	failures
https://www.usenix.org/system/files/conference/osdi14/osdi14-paper-yuan.pdf
77%	of	the	failures	
could	be reproduced	
by	a	unit	test
@PreusslerBerlin
74%	of	the	failures	
are	deterministic
@PreusslerBerlin
58%	of	the	catastrophic	failures	
where	error	handling
@PreusslerBerlin
st	by	PROTerry Johnston,	 CC	by	2.0;	flickr.com/photos/powerbooktrance/466709245
Fear	by	PRObrx0;	CC	by	2.0;	flickr.com/photos/atul666/5429073525
Documentation!
mutually	by	Bruce	Denis;	CC	by	2.0;	flickr.com/photos/f2point8/5896153192
Unit	tests	
slow me	down!?
Unit	tests	
slow me	down!?
Feature	development	
will	slow	down	
long	term
@PreusslerBerlin
Customers	will	loose	trust
@PreusslerBerlin
Code	written	in	rush	
will	be	thrown	away
@PreusslerBerlin
The	next	developer	
will	throw	it away
@PreusslerBerlin
The	value	that	separates	amateurs	from	
professionals	is	that	velocity	is	a	direct	function	
of	quality.
The	value	that	separates	amateurs	from	professionals	is	
that	velocity	is	a	direct	function	of	quality.	
Novices	believe	..	that	hacking	is	fast.
The	value	that	separates	amateurs	from	professionals	is	
that	velocity	is	a	direct	function	of	quality.	
Novices	believe	..	that	hacking	is	fast.
The	higher	the	quality,	the	faster	you	go.
The	value	that	separates	amateurs	from	professionals	is	
that	velocity	is	a	direct	function	of	quality.	
Novices	believe	..	that	hacking	is	fast.
The	higher	the	quality,	the	faster	you	go.
Every	time	you	trade	quality	for	speed,	you	slow	
down.	Every	time.
- Uncle	Bob
YOU are	the	expert!
@PreusslerBerlin
Don’t	let	someone	
tell	YOU how	to	code!
@PreusslerBerlin
The	only way	to	go	fast
is	to	go	well
@PreusslerBerlin
@PreusslerBerlin
Be	professional!
@PreusslerBerlin
YOU are	not	paid	
to	write legacy	code
@PreusslerBerlin
Don’t	ship	crap!
@PreusslerBerlin
Your	test code	
is	more	important	
than	production	code
@PreusslerBerlin
But	how	do	we	start?
Write	testable	code
@PreusslerBerlin
Inversion	of	Control
@PreusslerBerlin
void doSomething() {
new Something().doWhatISay();
}
Not	testable!
void doSomething(Something something) {
something.doWhatISay();
}
class MyClass {
@Inject Something something;
void doSomething() {
something.doWhatISay();
}
} Dependency	
Injection
Single responsibilities
Clean Code
@PreusslerBerlin
Respect	Law	of	Demeter
http://enterprisecraftsmanship.com/2016/09/29/law-of-demeter-and-immutability/ @PreusslerBerlin
Avoid	static
@PreusslerBerlin
Avoid singletons
@PreusslerBerlin
Wrap	all	the	things
@PreusslerBerlin
But	what	about	
Legacy	Code?
New features	
must	have	tests!
@PreusslerBerlin
Investigate	bugs	with
exploratory	tests
@PreusslerBerlin
Explore	code	with	
characterization	tests
@PreusslerBerlinhttps://michaelfeathers.silvrback.com/characterization-testing
Tests	will	change	
your	architecture	
step	by	step
@PreusslerBerlin
Untestable	code
is	bad	code!
@PreusslerBerlin
If	you
see	crap,	
replace it!
@PreusslerBerlin
Add	testing	in	feature	planning
@PreusslerBerlin
Strict	Definition	of	Done
@PreusslerBerlin
YOU should	sign	
the	manifesto!
@PreusslerBerlin
manifesto.softwarecraftsmanship.org
Test	Driven	Development
@PreusslerBerlin
TDD is	fun
@PreusslerBerlin
Pair	programming	is	fun
@PreusslerBerlin
Ever	tried	the	“evil	coder”?
Testing	is	fun
@PreusslerBerlin
Playground	for	languages	and	libraries
Don‘t ship crap!
@PreusslerBerlin
Thank you!
@PreusslerBerlin
We are hiring!
careers.gsa@vimn.com

Unit Testing on Android: why and how? DevFest Romania, Bucharest 2016