+ All Categories
Home > Technology > Clean Unit Test Patterns

Clean Unit Test Patterns

Date post: 20-Aug-2015
Category:
Upload: frank-appel
View: 3,271 times
Download: 1 times
Share this document with a friend
Popular Tags:
85
C l e a n U n i t T e s t P a t t e r n s F r a n k A p p e l Blog: www.codeaffine.com Email: [email protected] Twitter: @frank_appel
Transcript
Page 1: Clean Unit Test Patterns

C l e a n U n i t T e s t P a t t e r n s

F r a n k A p p e lBlog: www.codeaffine.comEmail: [email protected]: @frank_appel

Page 2: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 3: Clean Unit Test Patterns

Independent Software DeveloperBlogger (http://codeaffine.com/blog)

Stalwart of agile methods and TDD in particular

Who I am..

Page 4: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 5: Clean Unit Test Patterns

Why bother?

Page 6: Clean Unit Test Patterns

Why bother?Test of too many concepts

Page 7: Clean Unit Test Patterns

Why bother?

Page 8: Clean Unit Test Patterns

Why bother?Mix of integration and unit test

Page 9: Clean Unit Test Patterns

Why bother?

Page 10: Clean Unit Test Patterns

Why bother?

Missing of clean and recognizable test structure

Page 11: Clean Unit Test Patterns

Why bother?

Page 12: Clean Unit Test Patterns

Why bother?

Tight coupling of unit under test and dependencies

Page 13: Clean Unit Test Patterns

Why bother?

Page 14: Clean Unit Test Patterns

Why bother?

Poor maintainability and progression

Page 15: Clean Unit Test Patterns

Why bother?

Page 16: Clean Unit Test Patterns

Why bother?

Don't do it!

Page 17: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 18: Clean Unit Test Patterns

Test Structure

Four Phases Pattern

Page 19: Clean Unit Test Patterns

Test Structure

Four Phases Pattern

Setup (Fixture)

Page 20: Clean Unit Test Patterns

Test Structure

Four Phases Pattern

Setup (Fixture)

Exercise

Page 21: Clean Unit Test Patterns

Test Structure

Four Phases Pattern

Setup (Fixture)

Exercise

Verify

Page 22: Clean Unit Test Patterns

Test Structure

Four Phases Pattern

Setup (Fixture)

Exercise

Verify

Teardown: cleaning up the fixture in case it is persistent.

Page 23: Clean Unit Test Patterns

Test Structure

Four Phases Pattern

Setup (Fixture)

Exercise

Verify

Teardown: cleaning up the fixture in case it is persistent.

Page 24: Clean Unit Test Patterns

Test Structure

Setup Patterns

Page 25: Clean Unit Test Patterns

Test Structure

Setup Patterns

Inline Setup

Page 26: Clean Unit Test Patterns

Test Structure

Setup Patterns

Page 27: Clean Unit Test Patterns

Test Structure

Setup Patterns

Page 28: Clean Unit Test Patterns

Test Structure

Setup Patterns

Delegate Setup

Page 29: Clean Unit Test Patterns

Test Structure

Setup Patterns

Page 30: Clean Unit Test Patterns

Test Structure

Setup Patterns

Page 31: Clean Unit Test Patterns

Test Structure

Setup Patterns

Implicit Setup

Page 32: Clean Unit Test Patterns

Test Structure

Reusable Setup Helper

Object MotherTest Fixture Registry

Implementation either as stateless test helper class or, in case the helper holds references to fixture or SUT objects, as stateful test helper object.

Page 33: Clean Unit Test Patterns

Test Structure

Implicit Teardown

Teardown is all about housekeeping and adds no information at all to a particular test

Page 34: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

Traditional approach: ugly try-catch block, mix of phases

Page 35: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

Page 36: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

Expected Annotation Method: might swallow setup problems, limited verification capabilities

Page 37: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

Page 38: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

ExpectedException Rule: Verification definition before execution phase

Page 39: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

Page 40: Clean Unit Test Patterns

Test Structure

Corner Case Tests: Expected Exceptions

Usage of a little execution utility and Java 8 Lambda expressions

Page 41: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 42: Clean Unit Test Patterns

Isolation

Dependencies: SUT and DOCThe tested Unit is usually referred to as system under test (SUT)

Components the SUT depends on are denoted as depended-on component (DOC)

DOCs we cannot control might impede decent test verification

DOCs might also slow down test execution

DOC’s behavior may change unexpectedly e.g. due to the usage of a newer version of a third party library

Test related problems with DOCs

Page 43: Clean Unit Test Patterns

Isolation

Isolation – A Unit Tester’s SEP FieldTest concerns seperately and keep tests independent of each other!

Indirect Inputs and Outputs

Page 44: Clean Unit Test Patterns

Isolation

Test Double Patterns

A unit should be designed in a way that each DOC can be replaced by a so called Test Double, which is a lightweight stand-in component for the

DOC.

A DOC is provided using dependency injection or a service locator. This ensures a loosely coupled micro-architecture that allows replacement of

the DOC with the test double.

Page 45: Clean Unit Test Patterns

Isolation

Controlling Indirect Inputs with Stubs

Page 46: Clean Unit Test Patterns

Isolation

Controlling Indirect Inputs with Stubs

Page 47: Clean Unit Test Patterns

Isolation

Controlling Indirect Inputs with Stubs

Page 48: Clean Unit Test Patterns

Isolation

Controlling Indirect Inputs with Stubs

Page 49: Clean Unit Test Patterns

Isolation

Controlling Indirect Inputs with Stubs

Page 50: Clean Unit Test Patterns

Isolation

Indirect Output Verification with Spies

The spy records the number value of the invocation in the test double’s number field. This allows to verify the indirect output as shown here:

record

verify

Page 51: Clean Unit Test Patterns

Isolation

What About Mocks?

Page 52: Clean Unit Test Patterns

Isolation

What About Mocks?

Behavior Verifcation

Page 53: Clean Unit Test Patterns

Isolation

What About Mocks?

Behavior Verifcation

Invocation Verification

Page 54: Clean Unit Test Patterns

Isolation

What About Mocks?

Page 55: Clean Unit Test Patterns

Isolation

What About Mocks?

Page 56: Clean Unit Test Patterns

Isolation

Spy or Mock?

Mocks break the usual test structure

Behavior verification is somewhat hidden

but

Mocks provide a precise stacktrace to the failure cause

Page 57: Clean Unit Test Patterns

Isolation

Test Double Frameworks

JMock, EasyMock mock based

Mockito spy based

Page 58: Clean Unit Test Patterns

Isolation

Test Double Frameworks

Only create test doubles for types you own

(indication for integration tests and an abstracting adapter layer)

A test double should not return another test double

(potential violation of law of demeter)

Page 59: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 60: Clean Unit Test Patterns

Runners

Suite and Categories

Use @RunWith to specify a particular test processor

Page 61: Clean Unit Test Patterns

Runners

Suite and Categories

Page 62: Clean Unit Test Patterns

Runners

Suite and Categories

Page 63: Clean Unit Test Patterns

Runners

Parameterized Tests

Parameterized tests allow to run the same test against multiple data recordsprovided as instance field(s) of the test case

fields

Page 64: Clean Unit Test Patterns

Runners

Parameterized Tests

Page 65: Clean Unit Test Patterns

Runners

Parameterized Tests

Specification of the Parameterized test processor using @RunWith

Page 66: Clean Unit Test Patterns

Runners

Parameterized Tests

Page 67: Clean Unit Test Patterns

Runners

Parameterized Tests

Field initialization takes place by constructor injection. Each data record isprovided by a particular collector method annotated with @Parameters

data records

initializations

Page 68: Clean Unit Test Patterns

Runners

JUnitParams

Page 69: Clean Unit Test Patterns

Rules

What are JUnit Rules?Rules provide a possibility to intercept test method calls similar as an AOP framework

would do.

TemporaryFolder for example removes automatically all created files and directories after a test run.

A list of JUnit built-in Rules can be found at:https://github.com/junit-team/junit/wiki/Rules

Page 70: Clean Unit Test Patterns

Rules

How does it work?

y

Page 71: Clean Unit Test Patterns

Rules

How does it work?

Page 72: Clean Unit Test Patterns

Rules

How does it work?

Test execution produces:

beforeduringafter

Page 73: Clean Unit Test Patterns

Rules

How does it work?

Page 74: Clean Unit Test Patterns

Rules

How does it work?

Page 75: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 76: Clean Unit Test Patterns

Assertions

JUnit AssertThe built-in assertion mechanism of JUnit is provided by the class org.junit.Assert:

It is quite verbose and somewhat limited with respect to the expressiveness of assertions that require more complex predicates

Page 77: Clean Unit Test Patterns

Assertions

HamcrestA third-party library that claims to provide an API for creating flexible expressions of

intent is Hamcrest:

MatcherAssert.assertThat(...) evaluates the execution result (actual) against a predicate (matcher-expression)

MatcherAssert provides an overloaded assertThat method for failure message specification

Page 78: Clean Unit Test Patterns

Assertions

Hamcrest

Page 79: Clean Unit Test Patterns

Assertions

Hamcrest

Page 80: Clean Unit Test Patterns

Assertions

AssertJThe library AssertJ strives to improves verification by providing fluent assertions API:

Assertions.assertThat(...) verifies the execution result (actual) against fluently added conditions

The Assert instance provides the method describeAs(String) to specify a particular failure message

Page 81: Clean Unit Test Patterns

Assertions

AssertJ

Page 82: Clean Unit Test Patterns

Assertions

AssertJ

Page 83: Clean Unit Test Patterns

Assertions

Which one to use?

JUnit Assert is surely somewhat dated and less object-oriented

Hamcrest matchers provide a clean separation of assertion and predicate definition

AssertJ assertions score with a compact and easy to use programming style

Hamcrest and AssertJ support custom matchers/assertions for domain specific types

So now you are spoilt for choice…

Page 84: Clean Unit Test Patterns

Why bother?StructureIsolation

Runners and RulesAssertions

Q&A

C l e a n U n i t T e s t P a t t e r n s

Page 85: Clean Unit Test Patterns

C l e a n U n i t T e s t P a t t e r n s

ReferencesxUnit Test Patterns, Gerard Meszaros, 2007

Clean Code, Chapter 9: Unit Tests, Robert C. Martin, 2009

Growing Object-Oriented Software, Guided by Tests, Chapter 8, Steve Freeman, Nat Pryce, 2010

Practical Unit Testing with JUnit and Mockito, Appendix C. Test Spy vs. Mock, Tomek Kaczanowski, 2013

JUnit in a Nutshell: Yet Another JUnit Tutorial,http://www.codeaffine.com/2014/09/24/junit-nutshell-junit-tutorial, Frank Appel 2014

Clean JUnit Throwable-Tests with Java 8 Lambdas,http://www.codeaffine.com/2014/07/28/clean-junit-throwable-tests-with-java-8-lambdas/,Frank Appel 2014

F r a n k A p p e lBlog: www.codeaffine.comEmail: [email protected]: @frank_appel


Recommended