SlideShare uma empresa Scribd logo
1 de 38
Baixar para ler offline
/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

Mais conteúdo relacionado

Mais de Yegor Bugayenko

Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?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?
Are You Sure You Are Not a Micromanager?Yegor Bugayenko
 
On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)Yegor Bugayenko
 
My Experience of 1000 Interviews
My Experience of 1000 InterviewsMy Experience of 1000 Interviews
My Experience of 1000 InterviewsYegor 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?
Are you sure you are not a micromanager?Yegor Bugayenko
 
Quality Assurance vs. Testing
Quality Assurance vs. TestingQuality Assurance vs. Testing
Quality Assurance vs. TestingYegor Bugayenko
 
Typical Pitfalls in Testing
Typical Pitfalls in TestingTypical Pitfalls in Testing
Typical Pitfalls in TestingYegor Bugayenko
 
Software Testing Pitfalls
Software Testing PitfallsSoftware Testing Pitfalls
Software Testing PitfallsYegor Bugayenko
 
Five Trends We Are Afraid Of
Five Trends We Are Afraid OfFive Trends We Are Afraid Of
Five Trends We Are Afraid OfYegor Bugayenko
 
Who Cares About Quality?
Who Cares About Quality?Who Cares About Quality?
Who Cares About Quality?Yegor Bugayenko
 
Zold: a cryptocurrency without Blockchain
Zold: a cryptocurrency without BlockchainZold: a cryptocurrency without Blockchain
Zold: a cryptocurrency without BlockchainYegor Bugayenko
 
How to Cut Corners and Stay Cool
How to Cut Corners and Stay CoolHow to Cut Corners and Stay Cool
How to Cut Corners and Stay CoolYegor Bugayenko
 
Java Annotations Are a Bad Idea
Java Annotations Are a Bad IdeaJava Annotations Are a Bad Idea
Java Annotations Are a Bad IdeaYegor Bugayenko
 

Mais de Yegor Bugayenko (20)

Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?
 
Are You Sure You Are Not a Micromanager?
Are You Sure You Are Not a Micromanager?Are You Sure You Are Not a Micromanager?
Are You Sure You Are Not a Micromanager?
 
On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)
 
My Experience of 1000 Interviews
My Experience of 1000 InterviewsMy Experience of 1000 Interviews
My Experience of 1000 Interviews
 
Are you sure you are not a micromanager?
Are you sure you are not a micromanager?Are you sure you are not a micromanager?
Are you sure you are not a micromanager?
 
Quality Assurance vs. Testing
Quality Assurance vs. TestingQuality Assurance vs. Testing
Quality Assurance vs. Testing
 
Is Java Getting Better?
Is Java Getting Better?Is Java Getting Better?
Is Java Getting Better?
 
Typical Pitfalls in Testing
Typical Pitfalls in TestingTypical Pitfalls in Testing
Typical Pitfalls in Testing
 
Software Testing Pitfalls
Software Testing PitfallsSoftware Testing Pitfalls
Software Testing Pitfalls
 
Five Trends We Are Afraid Of
Five Trends We Are Afraid OfFive Trends We Are Afraid Of
Five Trends We Are Afraid Of
 
Experts vs Expertise
Experts vs ExpertiseExperts vs Expertise
Experts vs Expertise
 
Who Cares About Quality?
Who Cares About Quality?Who Cares About Quality?
Who Cares About Quality?
 
Quantity vs. Quality
Quantity vs. QualityQuantity vs. Quality
Quantity vs. Quality
 
Experts vs Expertise
Experts vs ExpertiseExperts vs Expertise
Experts vs Expertise
 
Zold: a cryptocurrency without Blockchain
Zold: a cryptocurrency without BlockchainZold: a cryptocurrency without Blockchain
Zold: a cryptocurrency without Blockchain
 
Life Without Blockchain
Life Without BlockchainLife Without Blockchain
Life Without Blockchain
 
How to Cut Corners and Stay Cool
How to Cut Corners and Stay CoolHow to Cut Corners and Stay Cool
How to Cut Corners and Stay Cool
 
Math or Love?
Math or Love?Math or Love?
Math or Love?
 
How much do you cost?
How much do you cost?How much do you cost?
How much do you cost?
 
Java Annotations Are a Bad Idea
Java Annotations Are a Bad IdeaJava Annotations Are a Bad Idea
Java Annotations Are a Bad Idea
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Object-Oriented JUnit Tests