42
Practicing Advanced Unit Testing v2.3.2 (25.05.2022) with the «Trading Card Game» Kata Battlecry: Draw a slide. Deathrattle: Transform audience into 1/1 sheep. Speaker

Practicing Advanced Unit Testing

Embed Size (px)

DESCRIPTION

Doing Code Katas alone or in a Dojo can help sharpen our elementary skills as software developers. Practicing IDE shortcuts and TDD mini-step cycles is very useful for the daily business, yet I find some existing Code Katas too far away from real-life programming situations. That’s why I came up with the Trading Card Game Kata – which is (very loosely) based on Blizzard Entertainment’s free-to-play online-game “Hearthstone – Heroes of Warcraft”. This Kata is focused on practicing TDD in a slightly more complex (but not complicated) situation where you might have to think about rules like Single Responsibility Principle or Command Query Separation and might even feel the urge to use a Mocking framework at some point. The rules are kept very simple to begin with but can be extended easily to make the Kata more challenging or concentrate on specific aspects of software development. In this talk I will first introduce the ideas of Katas in general and explain the TCG Kata rules to you. Then I will present some real-life best practices for writing good developer tests, using my TCG Kata sample solutions as a showcase. This will include: * Picking the right Test Double * Test Data Builders * Behavior Tests with BDDMockito * Prose-like Assertions with Hamcrest * Readability Sugar * Custom Matchers against Test Diabetes The full Kata ruleset and sample solutions in Java 8 (JUnit4, Mockito, Hamcrest) and Groovy (Spock) can be found on https://github.com/bkimminich/kata-tcg. A recording of this talk at „Agile Saturday X“ in Tallinn, Estonia can be found on https://vimeo.com/92886146.

Citation preview

Page 1: Practicing Advanced Unit Testing

Practicing AdvancedUnit Testing

v2.3.2 (13.04.2023)

with the«Trading Card Game»

Kata

Battlecry: Draw a slide.Deathrattle: Transformaudience into 1/1 sheep.

Speaker

Page 2: Practicing Advanced Unit Testing

Kata

Kata ( 型 or 形 literally: "form”) is a Japanese word describing detailed

choreographed patterns of movements practiced either solo or in pairs.

Page 3: Practicing Advanced Unit Testing

Trading Card Game (TCG)

A […] trading card game (TCG) […] is a card game that uses specially designed sets of playing cards […] mass-produced for trading or 

collectibility, and it must have rules for strategic game play. Acquiring these cards may be done by trading with other players or buying card 

packs.

Page 4: Practicing Advanced Unit Testing

Why invent another Kata?

Bro, why U not use

?????

Rules not extensible

Not TDD-friendly

Too easy

Page 5: Practicing Advanced Unit Testing

Hearthstone: Heroes of Warcraft®

Page 6: Practicing Advanced Unit Testing

Kata Trading Card Game

Page 7: Practicing Advanced Unit Testing

Player Setup

0 0 1 2 2 2 3 3

3 4 4 4 5 6 6 7 8

30 0/0

Health ManaD

eck

1 3

5

Hand

Page 8: Practicing Advanced Unit Testing

2

7

Active Player

1 3 5

8

0/11/1

Gameplay

2

30 300/0

3Play Card 1

29

Cause 1

Damage

0/0

Draw Card

-1

Receive +1

Mana Slot

Replenish

ManaPay 1

Mana

Game Table

Draw extra Card

Page 9: Practicing Advanced Unit Testing

2 4

0/1

Forced Turn Skip

25 8

30 1/1

23

29

3

No affordable Cards

2/2

7

Page 10: Practicing Advanced Unit Testing

29

4

0/2

Ongoing Gameplay

25 8

30 1/1

23

27

3 4

2

2

-2

2/2

7

Page 11: Practicing Advanced Unit Testing

12

4

8/8

Overload Rule

75 8

18 7/7

8016

2

Discard Card when Hand

already holds 5 Cards 7

Page 12: Practicing Advanced Unit Testing

410/10

Mana Cap

5

8 10/10

166 Mana is capped at 10 Slots

Page 13: Practicing Advanced Unit Testing

410/10

Bleedout Rule

5

8 10/10

166Drawing

from empty Deck

7-1

Cause 1

Damage

Page 14: Practicing Advanced Unit Testing

410/10

Game End

5

10/10

166

7 -2-6

Winner! Loser!

4/10

Page 15: Practicing Advanced Unit Testing

Kata TCG Rules & Variations

https://github.com/bkimminich/kata-tcg

Detailed rules descriptionAdvanced Variations

Healing cardsUse cards as MinionsDifferent cost & damageCard drawer cardsDeck customization

Page 16: Practicing Advanced Unit Testing

Kata TCG Sample Solution in Java

https://github.com/bkimminich/kata-tcg

Java 8Lambdas & Stream API

JunitMockito

For handling dependencies of tested objects

HamcrestMatchers for better legibility in assertions

System RulesJUnit @Rules for substituting java.lang.System

Page 17: Practicing Advanced Unit Testing

Other Sample Solutions

https://github.com/bkimminich/kata-tcg

GroovySpock

JavaScriptKarmaJasminePhantomJS

Page 18: Practicing Advanced Unit Testing

Advanced Unit TestingExamples

Page 19: Practicing Advanced Unit Testing

Skip No-Brainers

Compile Error as Red Test

Red

Green

Refactor

Page 20: Practicing Advanced Unit Testing

Obvious Implementation

Simplest possible

step

Simplest useful step

Page 21: Practicing Advanced Unit Testing

Specify the Object under Test

Irrelevant Details

Setter introduced for

testing

Implementation Detail

Assuming Hidden Default

Page 22: Practicing Advanced Unit Testing

Define the Object under Test

Another unwanted

setter

Hidden default problem solved

Page 23: Practicing Advanced Unit Testing

Builder Pattern

Reads like natural

language

Explicitly listed Expectations

No unnecessary details

…?

Page 24: Practicing Advanced Unit Testing

Builder Internals

Sensible Default Values

Package visible full Constructor

needed

Page 25: Practicing Advanced Unit Testing

Fluent API

Setting properties via

fluent API

Page 26: Practicing Advanced Unit Testing

Mocking Behavior

Interface has no implementation

yet

Mock Behavior for

this Test

Page 27: Practicing Advanced Unit Testing

Trashcan RefactoringCardPicker turned

out to be overengineered

Mocking

Page 28: Practicing Advanced Unit Testing

Mocking BDD Style

Can beconfused withGiven-When-

Then part

Can beconfused withGiven-When-Then part

Page 29: Practicing Advanced Unit Testing

Mocking BDD Style

Sugar coating for BDD syntax

Matches its meaning inGiven-When-Then

structure

Page 30: Practicing Advanced Unit Testing

Avoid Redundancy

Redundant & spoiled with Implementation

Details

Another slight redundancy

Page 31: Practicing Advanced Unit Testing

Avoid Constants

Disturbs the reading flow

Page 32: Practicing Advanced Unit Testing

Syntactic Sugar

Improves Legibility and

removes Redundancy

Page 33: Practicing Advanced Unit Testing

More Syntactic Sugar

Magic Number

sDifferent Level of Abstraction

Page 34: Practicing Advanced Unit Testing

Syntactic Artificial Sweetener

No-Op SyntacticSugar for Legibility

Syntactic Sugar

reduces Redundancy

Page 35: Practicing Advanced Unit Testing

Test DiabetesToo much Sugar is bad for your

Test

Customer Matchers can medicate this

Page 36: Practicing Advanced Unit Testing

Custom MatcherEncapsulation of

Expectation

Produces helpful Error

Messages

Even better with just a little bit of

Sugar

Page 37: Practicing Advanced Unit Testing

Demo | Q&A

Page 38: Practicing Advanced Unit Testing

Demo: Test Execution

All tests passPretty fast

Page 39: Practicing Advanced Unit Testing

Demo: Code Coverage

Page 40: Practicing Advanced Unit Testing

Ugly UIUgly Retro UI

Demo: Human vs. AI Game

Player Inputvia System.in

AI beats me! Again!

Page 41: Practicing Advanced Unit Testing

Thank you for your attention!

Recording of this talk at „Agile Saturday X“ in Tallinn, Estoniahttps://vimeo.com/92886146

https://twitter.com/bkimminichhttps://linkedin.com/in/bkimminichhttps://google.com/+BjörnKimminichhttp://slideshare.net/BjrnKimminich/

Page 42: Practicing Advanced Unit Testing

Credits

Background Image: Demonplay (Permission of

use granted)

Other Images:Blizzard Entertainment (Permission of use requested)

Wikipedia, Github, Agile EstoniaAllmystery.de, Gueball.de, Natekohl.net,

Scenicreflections.com

Hearthstone: Heroes of Warcraft® is a trademark or registered trademark of Blizzard Entertainment, Inc., in the U.S. and/or other countries.