Object-Oriented JUnit Tests

/38@yegor256 1
Yegor Bugayenko
Object-Oriented JUnit Tests
/38@yegor256 2
Why OOP?
/38@yegor256 3
yegor256/hangman
11
@funivan
/38@yegor256 4
Volume 2
$20
Free shipment
/38@yegor256 5
Unit testing anti-patterns:
Happy Path Tests
Validation and Boundary
Easy Tests
The Giant
The Cuckoo
The Conjoined Twins
The Slow Poke
Anal Probe
Test It All
Line Hitter
Wait and See
The Silent Catcher
Chain Gang
The Mockery
The Free Ride
The Local Hero
Wet Floor
The Flickering Test
The Environmental Vandal
Second Class Citizens
The Secret Catcher
Logic in Tests
Code Matching
Misleading Tests
Not Asserting
Asserting on Not-Null
Forty Foot Pole
Test With No Name
Excessive Setup
The Dead Tree
The Turing Test
The Mother Hen
The Sleeper
The Butterfly
Doppelgänger
The Inspector
Structured Inspection
The Cupcake
/38@yegor256
Dependencies
6
are evil
Unbreakable
/38@yegor256 7
class Book {
private int id;
String title() {
return Database.getInstance().fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
} String title = new Book().title();
/38@yegor256 8
Book
BookTest
DatabaseSomething
/38@yegor256 9
class Book {
private int id;
String title() {
return DbUtils.fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
}
static
Johan Haleby
/38@yegor256 10
class Book {
private int id;
String title() {
return Database.getInstance().fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
}
global
/38@yegor256 11
class DbUtils {
private static int port;
static String fetch(String sql) {
// ..
}
}
class Database {
private static Database INSTANCE;
private int port;
private Database() {}
static Database getInstance() {
if (Database.INSTANCE == null) {
Database.INSTANCE = new Database();
}
return Database.INSTANCE;
}
String fetch(String sql) {
// ..
}
}
/38@yegor256 12
class DbUtils {
private static int port;
static String fetch(String sql) {
// ..
}
}
class Database {
private static Database INSTANCE;
private int port;
private Database() {}
static Database getInstance() {
if (Database.INSTANCE == null) {
Database.INSTANCE = new Database();
}
return Database.INSTANCE;
}
static void setInstance(Database db) {
Database.INSTANCE = db;
}
String fetch(String sql) {
// ..
}
}
/38@yegor256 13
class Book {
private int id;
String title() {
return new Database().fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
}
new
/38@yegor256 14
class Book {
private int id;
private Database database;
Book() {
this(new Database());
}
Book(Database db) {
this.database = db;
}
String title() {
return this.database.fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
}
DependencyInjection
/38@yegor256 15
new Book(new NotRealDatabase());
class NotRealDatabase implements Database {
// ...
}
interfaces
@Override
/38@yegor256 16
static
evil!
singletons
new
interface-less
/38@yegor256
Mocking
17
is evil
Ad hoc
/38@yegor256 18
static import org.mockito.Mockito.*;
class BookTest {
@Test
public canFetchTitleFromDatabase() {
Database db = mock(Database.class);
doReturn(“UML Distilled”).when(db).fetch(
“SELECT title FROM book WHERE id=?”, 123
);
Book book = new Book(db, 123);
assert book.title().equals(“UML Distilled”);
}
}
/38@yegor256 19
mocking
algorithm
/38@yegor256 20
Book
BookTest
db
assumptions
/38@yegor256 21
Safety net.
(c) By John M, CC BY-SA 2.0
/38@yegor256 22
static import org.mockito.Mockito.*;
class BookTest {
@Test
public canFetchTitleFromDatabase() {
Database db = mock(Database.class);
doReturn(“UML Distilled”).when(db).fetch(
“SELECT title FROM book WHERE id=?”, 123
);
Book book = new Book(db, 123);
assert book.title().equals(“UML Distilled”);
}
}
class Book {
private int id;
private Database database;
Book(Database db, int i) {
this.database = db;
this.id = i;
}
String title() {
return this.database.fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
}
/38@yegor256 23
False positives.
/38@yegor256 24
Fakes or doubles.
/38@yegor256 25
class BookTest {
@Test
public canFetchTitleFromDatabase() {
Book book = new Book(
new FakeDatabase(), 123
);
assert book.title().equals(“UML Distilled”);
}
}
class Book {
private int id;
private Database database;
Book(Database db, int i) {
this.database = db;
this.id = i;
}
String title() {
return this.database.fetch(
“SELECT title FROM book WHERE id=?”,

this.id
);
}
}
/38@yegor256 26
class BookTest {
@Test
public canFetchTitleFromDatabase() {
Book book = new Book(
new FakeDatabase()
.withTitle(123, “UML Distilled”)
.withTitle(1, “Elegant Objects”)
.withUser(“Peter Pan”),
123
);
assert book.title().equals(“UML Distilled”);
}
}
/38@yegor256 27
Ad hoc mocking.
Fakes.
/38@yegor256
Algorithms
28
are evil
In-method
Maybe!
/38@yegor256 29
cactoos
www.cactoos.org
/38@yegor256 30
class BookTest {
@Test
public canBuildURL() {
Book book = new Book();
book.setLanguage(Locale.RUSSIAN);
book.setEncoding(“UTF-8”);
book.setTitle(“Дон Кихот”);
assert book.getURL().contains(“%D0%94%”);
}
}
procedural!
/38@yegor256 31
Hamcrest
An anagram for “matchers”.
/38@yegor256 32
A test method must have nothing
but a single statement:
assertThat()
/38@yegor256 33
assertThat(book, hasTitle(“UML Distilled”));
“We assert that the book behaves as if it has a title.”
/38@yegor256 34
assertThat(
book,
hasURL(
hasPath(
containsString(
equalTo(“%D0%94%”)
)
)
)
);
We assert that

the book behaves as if
it has a URL that behaves as if
it has a path that behaves as if
it contains a string that behaves as if
it is equal to “%D0%94%”.
/38@yegor256 35
/38@yegor256 36
/38@yegor256 37
shorter test methods
why?!
immutable objects
share matchers
/38@yegor256 38
Quality Award.
$4096
1 of 38

Recommended

Object-Oriented Flavor for JUnit Tests by
Object-Oriented Flavor for JUnit TestsObject-Oriented Flavor for JUnit Tests
Object-Oriented Flavor for JUnit TestsYegor Bugayenko
691 views31 slides
Change Anything with Cucumber and ATDD by
Change Anything with Cucumber and ATDDChange Anything with Cucumber and ATDD
Change Anything with Cucumber and ATDDmpmenne
720 views57 slides
#codemotion2016: Everything you should know about testing to go with @pedro_g... by
#codemotion2016: Everything you should know about testing to go with @pedro_g...#codemotion2016: Everything you should know about testing to go with @pedro_g...
#codemotion2016: Everything you should know about testing to go with @pedro_g...Sergio Arroyo
694 views84 slides
Some testing - Everything you should know about testing to go with @pedro_g_s... by
Some testing - Everything you should know about testing to go with @pedro_g_s...Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Sergio Arroyo
1.4K views82 slides
Java Advanced Features by
Java Advanced FeaturesJava Advanced Features
Java Advanced FeaturesMichael Redlich
250 views47 slides
Generating characterization tests for legacy code by
Generating characterization tests for legacy codeGenerating characterization tests for legacy code
Generating characterization tests for legacy codeJonas Follesø
1.6K views30 slides

More Related Content

More from Yegor Bugayenko

Can Distributed Teams Deliver Quality? by
Can Distributed Teams Deliver Quality?Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?Yegor Bugayenko
269 views23 slides
Are You Sure You Are Not a Micromanager? by
Are You Sure You Are Not a Micromanager?Are You Sure You Are Not a Micromanager?
Are You Sure You Are Not a Micromanager?Yegor Bugayenko
245 views16 slides
On Requirements Management (Demotivate Them Right) by
On Requirements Management (Demotivate Them Right)On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)Yegor Bugayenko
220 views16 slides
My Experience of 1000 Interviews by
My Experience of 1000 InterviewsMy Experience of 1000 Interviews
My Experience of 1000 InterviewsYegor Bugayenko
219 views20 slides
Are you sure you are not a micromanager? by
Are you sure you are not a micromanager?Are you sure you are not a micromanager?
Are you sure you are not a micromanager?Yegor Bugayenko
251 views15 slides
Quality Assurance vs. Testing by
Quality Assurance vs. TestingQuality Assurance vs. Testing
Quality Assurance vs. TestingYegor Bugayenko
660 views25 slides

More from Yegor Bugayenko(20)

Can Distributed Teams Deliver Quality? by Yegor Bugayenko
Can Distributed Teams Deliver Quality?Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?
Yegor Bugayenko269 views
Are You Sure You Are Not a Micromanager? by Yegor Bugayenko
Are You Sure You Are Not a Micromanager?Are You Sure You Are Not a Micromanager?
Are You Sure You Are Not a Micromanager?
Yegor Bugayenko245 views
On Requirements Management (Demotivate Them Right) by Yegor Bugayenko
On Requirements Management (Demotivate Them Right)On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)
Yegor Bugayenko220 views
My Experience of 1000 Interviews by Yegor Bugayenko
My Experience of 1000 InterviewsMy Experience of 1000 Interviews
My Experience of 1000 Interviews
Yegor Bugayenko219 views
Are you sure you are not a micromanager? by Yegor Bugayenko
Are you sure you are not a micromanager?Are you sure you are not a micromanager?
Are you sure you are not a micromanager?
Yegor Bugayenko251 views
Zold: a cryptocurrency without Blockchain by Yegor Bugayenko
Zold: a cryptocurrency without BlockchainZold: a cryptocurrency without Blockchain
Zold: a cryptocurrency without Blockchain
Yegor Bugayenko282 views
How to Cut Corners and Stay Cool by Yegor Bugayenko
How to Cut Corners and Stay CoolHow to Cut Corners and Stay Cool
How to Cut Corners and Stay Cool
Yegor Bugayenko318 views

Recently uploaded

Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O... by
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...ShapeBlue
59 views13 slides
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITShapeBlue
138 views8 slides
MVP and prioritization.pdf by
MVP and prioritization.pdfMVP and prioritization.pdf
MVP and prioritization.pdfrahuldharwal141
39 views8 slides
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueShapeBlue
134 views54 slides
Network Source of Truth and Infrastructure as Code revisited by
Network Source of Truth and Infrastructure as Code revisitedNetwork Source of Truth and Infrastructure as Code revisited
Network Source of Truth and Infrastructure as Code revisitedNetwork Automation Forum
49 views45 slides
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or... by
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...ShapeBlue
128 views20 slides

Recently uploaded(20)

Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O... by ShapeBlue
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
ShapeBlue59 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue138 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue134 views
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or... by ShapeBlue
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
ShapeBlue128 views
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely76 views
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool by ShapeBlue
Extending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPoolExtending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPool
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool
ShapeBlue56 views
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by ShapeBlue
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
ShapeBlue48 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue149 views
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by ShapeBlue
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
ShapeBlue105 views
State of the Union - Rohit Yadav - Apache CloudStack by ShapeBlue
State of the Union - Rohit Yadav - Apache CloudStackState of the Union - Rohit Yadav - Apache CloudStack
State of the Union - Rohit Yadav - Apache CloudStack
ShapeBlue218 views
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by ShapeBlue
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
ShapeBlue52 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue154 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue93 views
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue113 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc130 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue by ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
ShapeBlue75 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue86 views

Object-Oriented JUnit Tests