advertisement

Practicing Advanced Unit Testing

50 %
50 %
advertisement
Information about Practicing Advanced Unit Testing
Technology

Published on February 15, 2014

Author: BjrnKimminich

Source: slideshare.net

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.

First I will introduce the ideas of Katas and Dojos in general and explain the TCG Kata rules to you. Then I will demo some real-life best-practices for writing good developer tests, using my TCG Kata sample solution 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

The full Kata ruleset and a sample solution in Java 8 can be found on https://github.com/bkimminich/kata-tcg.
advertisement

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

Kata Kata (型 or 形 literally: "form”) is a Japanese word describing detailed choreographed patterns of movements practiced either solo or in pairs.

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.

Why invent another Kata? Bro, why U not use Not TDDfriendly ?? ? ? ? Rules not extensible Too easy

Hearthstone: Heroes of Warcraft®

Kata Trading Card Game

Player Setup Health 30 Mana Hand 0/0 3 4 4 4 5 5 6 6 7 8 Deck 0 0 1 1 2 2 2 3 3 3

Gameplay Receive +1 ReplenishActive Slot Mana Mana Player Pay 1 Mana 0/1 1/1 30 0/0 1 3 5 8 Play Card 1 Draw Card Cause 1 Damage 0/0 29 30 -1 3 2 2

Forced Turn Skip 30 2/2 0/1 1/1 3 5 8 2 29 3 2 2 No affordable Cards 4

Ongoing Gameplay 2/2 30 0/2 1/1 3 5 8 2 4 3 2 2 2 27 29 -2 4

Overload Rule 8/8 18 5 8 6 1 7 2 7/7 Discard Card when Hand already holds 5 Cards 12 0 8 4

Mana Cap 8 10/10 5 6 Mana is capped at 10 Slots 10/10 4 6 1

Bleedout Rule Cause 1 Damage 7 -8 10/10 1 5 6 Drawing from empty Deck 10/10 4 6 1

Game End 7 5 6 10/10 4/10 Winner! 10/10 -6 -2 4 Loser! 6 1

Kata TCG Rules & Variations https://github.com/bkimminich/kata-tcg Detailed rules description Advanced Variations Different cost & damage Healing cards Card drawer cards Use cards as Minions Deck customization

Kata TCG Sample Solution https://github.com/bkimminich/kata-tcg Java 1.8.0 Early Access Lambdas & Stream API Junit 4.11 Mockito 1.9.5 For handling dependencies of tested objects Hamcrest 1.3 Matchers for better readability in assertions System Rules 1.4.0 JUnit @Rules for substituting java.lang.System

Advanced Unit Testing Examples

Skip No-Brainers … Compile Error as Red Test Red Refactor Green

Obvious Implementation Simplest useful step Simplest possible step

Specify the Object under Test Irrelevant Details Implementation Detail Setter introduced for testing Assuming Hidden Default

Define the Object under Test Hidden default problem solved Another unwanted setter

Builder Pattern Prose-like Readability Explicitly listed Expectations …? No unnecessary details

Builder Internals Sensible Default Values Package visible full Constructor needed

Fluent API Setting properties via fluent API

Mocking Behavior Interface has no implementation yet Mock Behavior for this Test

Trashcan Refactoring CardPicker turned out to be overengineered Mocking

Mocking BDD Style Can be confused with Given-When-Then part Can be confused with Given-When-Then part

Mocking BDD Style Sugar coating for BDD syntax Matches its meaning in Given-When-Then structure

Avoid Redundancy Redundant & spoiled with Implementation Details Another slight redundancy

Avoid Constants Disturbs the reading flow

Syntactic Sugar Improves Readability and removes Redundancy

More Syntactic Sugar Magic Numbers Different Level of Abstraction

Syntactic Artificial Sweetener No-Op Syntactic Sugar for Readability Syntactic Sugar reduces Redundancy

Demo | Q&A

Demo: Test Execution Pretty fast All tests pass

Demo: Code Coverage

Demo: Human vs. AI Game Ugly Retro UI Ugly UI Player Input via System.in AI beats me! Again! 

Thank you for your attention! https://twitter.com/bkimminich https://linkedin.com/in/bkimminich https://google.com/+BjörnKimminich http://slideshare.net/BjrnKimminich/

Credits Background Image: Demonplay (Permission of use granted) Other Images: Blizzard Entertainment (Permission of use requested) Wikipedia, Github, Agile Estonia Allmystery.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.

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Practicing Advanced Unit Testing - DOAG Deutsche ORACLE ...

Practicing Advanced Unit Testing v2.3.2 (13.03.2015) withthe «Trading Card Game» Kata Battlecry:Draw a slide. Deathrattle:Transform audience into 1/1 sheep.
Read more

Björn Kimminich - Practicing Advanced Unit Testing with ...

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 ...
Read more

Practicing Advanced Unit Testing with the TCG Kata | Björn ...

Doing Code Katas alone or in a Dojo can help sharpen our elementary skills as software developers. Practicing IDE shortcuts and TDD mini-step ...
Read more

Unit Testing Slide decks | Lanyrd

Unit Testing Slide decks. ... from Practicing Advanced Unit Testing with the TCG Kata at JavaLand 2015, 24th-25th March 2015 Added 1 year ago.
Read more

unit test | Björn Kimminich's Blog

Practicing Advanced Unit Testing with the TCG Kata. Doing Code Katas alone or in a Dojo can help sharpen our elementary skills as software developers.
Read more

Unit Testing with Visual Studio | QA

Unit Testing with Visual Studio. ... Practicing TDD within Visual Studio ; ... Organizing unit tests; Module 5: Advanced Unit Testing in Visual Studio
Read more

Programming conferences and events | Lanyrd

Browse Programming conferences and events around the world. ... Practicing Advanced Unit Testing with th... video for. Practicing Advanced Unit Testing ...
Read more

Trainingensite - Advanced Unit Testing with .NET

Learn how to write Unit Tests that helps you forward Course outline. Be more productive when practicing Test-Driven Development (TDD) by leveraging ...
Read more

Agile Estonia on Vimeo

Björn Kimminich - Practicing Advanced Unit Testing with the TCG Kata . 2 years ago. Anton Keks - In Agile Right Tools Matter . 2 years ago. See all . Vimeo.
Read more