BW12	
Session	
6/8/16	4:15	PM	
	
	
	
	
	
	
Identify	and	Exploit	Behavioral	
Boundaries	for	Unit	Testing	
	
Presented	by:	
	
Rob	Myers	
Agile	Institute	
	
	
Brought	to	you	by:		
		
	
	
	
	
350	Corporate	Way,	Suite	400,	Orange	Park,	FL	32073		
888---268---8770	··	904---278---0524	-	info@techwell.com	-	http://www.techwell.com/
Rob	Myers	
Agile	Institute	
	
The	founder	of	Agile	Institute,	Rob	Myers	teaches	courses	that	are	always	a	blend	
of	fun	and	practical	hands-on	labs,	“Training	From	the	Back	of	the	Room”	learning	
techniques,	and	relevant	first-person	stories	from	both	successful	and	not-so-
successful	agile	implementations.	With	thirty	years	of	professional	experience	
with	software	development	teams,	Rob	has	been	training	and	coaching	
organizations	in	Scrum	and	Extreme	Programming	since	1998.	He	currently	works	
with	tiny	start-ups	and	huge	Fortune	100	multinationals,	helping	them	with	
cultural	change	and	essential	practices	from	Scrum,	kanban,	XP,	and	lean.
6/5/16	
1	
5 June 2016 © Agile Institute 2008-2016 1
Identify & Exploit
Behavioral
Boundaries
for
Unit Testing
Rob Myers
@agilecoach
Better Software West
08 June 2016
Café!
5 June 2016 © Agile Institute 2008-2016 2
Unit Test
A test of one smallest bit of
behavior.
Also known these days as a
microtest.
6/5/16	
2	
5 June 2016 © Agile Institute 2008-2016 3
5 June 2016 © Agile Institute 2008-2016 4
6/5/16	
3	
5 June 2016 © Agile Institute 2008-2016 5
5 June 2016 © Agile Institute 2008-2016 6
“As the tests get more specific,
the code gets more generic.”
– Bob Martin
6/5/16	
4	
5 June 2016 © Agile Institute 2008-2016 7
“The more code you write
without testing the behavioral
boundaries, the harder they are
to find & test later.”
– me
triangulation
5 June 2016 © Agile Institute 2008-2016 8
6/5/16	
5	
5 June 2016 © Agile Institute 2008-2016 9
5 June 2016 © Agile Institute 2008-2016 10
dial setting
percentmanufacturermaximum
6/5/16	
6	
Pretend java.util.Set is BROKEN!
We need our own Set class with the
capabilities of creating unions & intersections;
checking if a set is empty; and also if it is equal
to, a superset of, or a subset of another set.
5 June 2016 © Agile Institute 2008-2016 11
5 June 2016 © Agile Institute 2008-2016 12
empty?
6/5/16	
7	
5 June 2016 © Agile Institute 2008-2016 13
@Test	
public	void	isEmptyWhenConstructed()	{	
	Set	set	=	new	Set();	
	assertThat(set.isEmpty(),	is(	true	));	
}	
5 June 2016 © Agile Institute 2008-2016 14
not empty
3
6/5/16	
8	
5 June 2016 © Agile Institute 2008-2016 15
@Test	
public	void	isNotEmptyWhenItemAdded()	{	
	Set	set	=	new	Set();	
	set.addElement(	3	);	
	assertThat(set.isEmpty(),	is(	false	));	
}	
	
@Test	
public	void	isEmptyWhenConstructed()	{	
	Set	set	=	new	Set();	
	assertThat(set.isEmpty(),	is(	true	));	
}	
5 June 2016 © Agile Institute 2008-2016 16
@Test(expected=IllegalArgumentException.class)	
public	void	shouldNotAcceptNegativeNumbers()	{	
	Set	set	=	new	Set();	
	set.addElement(	-1	);	
}	
-1
6/5/16	
9	
5 June 2016 © Agile Institute 2008-2016 17
A
B
1 2
3
5
84
6
7
9
5 June 2016 © Agile Institute 2008-2016 18
6/5/16	
10	
5 June 2016 © Agile Institute 2008-2016 19
boolean	subsetOf(Set	other)	
{	
	return	size()	<=	other.size();	
}		
	
	
boolean	supersetOf(Set	other)	
{	
	return	size()	>=	other.size();	
}		
	
5 June 2016 © Agile Institute 2008-2016 20
A
B
1 2
3
5
84
6
7
9
C
10
11
12
6/5/16	
11	
5 June 2016 © Agile Institute 2008-2016 21
Hungover Intern Principle
We write the microtests that will
warn a hung-over intern six months
from now as to why the
implementation is the way it is—
however simple or complex, efficient
or slow it may be.
*Protects against mistakes, not sabotage.
*
In order to be an acceptable password, a string
must:
q Have a length greater than 7 characters.
q Contain at least one alphabetic character.
q Contain at least one digit.
q Contain a special character.
q First character must be special or digit.
5 June 2016 © Agile Institute 2008-2016 22
Password Strength Checker
6/5/16	
12	
5 June 2016 © Agile Institute 2008-2016 23
12345678
Good Bad
1234567
1234567B 12345678
Rule
Length > 7
Has alpha
passw0rd passwordHas digit
passw0rd? passw0rdHas special
?passw0rd passw0rd?1st special/
digit 4password?
5 June 2016 © Agile Institute 2008-2016 24
?passw0rd
Good Bad
?passw0
?passw0rd ?1234567
Rule
Length > 7
Has alpha
?passw0rd ?passwordHas digit
?passw0rd passw0rdHas special
?passw0rd passw0rd?1st special/
digit 4password?
6/5/16	
13	
combinations
5 June 2016 © Agile Institute 2008-2016 25
X Y Z
A C E
A D E
A C F
A D F
B C E
B D E
B C F
B D F
pair-wise testing
5 June 2016 © Agile Institute 2008-2016 26
X Y Z
A C E
A D E
A C F
A D F
B C E
B D E
B C F
B D F
X Y Z
A C E
A D F
B D E
B C F
6/5/16	
14	
5 June 2016 © Agile Institute 2008-2016 27
Listen to the Microtests
If you have difficulty unit-testing
something,
this is indicating an opportunity to
improve the design.
5 June 2016 © Agile Institute 2008-2016 28
Rule
+ bool passes(string pw)
PasswordStrengthChecker
+ bool isStrong(
string password)
LengthGT7 HasDigitHasAlpha
HasSpecial FirstSpecialOrDigit
6/5/16	
15	
5 June 2016 © Agile Institute 2008-2016 29
12345678
Good Bad
1234567
A3, 3A, 3A3 3
Rule
Length > 7
Has alpha
3A, A3, A3A AHas digit
AB$, A$B AHas special
$A, 3A A$, A31st special/
digit
5 June 2016 © Agile Institute 2008-2016 30
Rule
+ bool passes(string pw)
PasswordStrengthChecker
+ bool isStrong(
string password)
LengthGT7 HasDigitHasAlpha
HasSpecial FirstSpecialOrDigit
6/5/16	
16	
5 June 2016 © Agile Institute 2008-2016 31
Rule
+ bool passes(string pw)
PasswordStrengthChecker
+ bool isStrong(
string password)
MockRule
+ bool passes(string pw)
+ void setPassesTo(bool p)
+ bool passesWasCalled()
LengthGT7 HasAlpha
HasSpecial
5 June 2016 © Agile Institute 2008-2016 32
ExamplesBoundary type
True / False
Valid / Invalid
Happy / Unhappy
Conditional
Iterative
Comparative Greater Than
Less Than
Before / After
Zero / One / Many
Subtype
*
* Examples! Not intended as a comprehensive list.
6/5/16	
17	
5 June 2016 © Agile Institute 2008-2016 33
5 June 2016 © Agile Institute 2008-2016 34
Rob.Myers@agileInstitute.com
https://www.linkedin.com/in/robmyers64
@agilecoach

Identify and Exploit Behavioral Boundaries for Unit Testing