Slide 1

Slide 1 text

Test-Driven Architecture Cherif BOUCHELAGHEM

Slide 2

Slide 2 text

Agenda - Architectural patterns - What about MVC? - Shared principles (between architectural patterns) - Test-Driven Development - The approach - Outside-In development - Demo (problem and code example)

Slide 3

Slide 3 text

architecturAL Patterns?

Slide 4

Slide 4 text

Architectural patterns Clean architecture - 2012 Functional core, imperative shell - 2012

Slide 5

Slide 5 text

Architectural patterns Onion architecture - 2008 Hexagonal architecture - 2005

Slide 6

Slide 6 text

What about MVC?

Slide 7

Slide 7 text

What about MVC? MVC is the half of the user menta model soure

Slide 8

Slide 8 text

What about MVC? MVC is the half of the user menta model DCI is the other half soure

Slide 9

Slide 9 text

What about MVC? Trygve Reenskaug James Coplien

Slide 10

Slide 10 text

Shared principles

Slide 11

Slide 11 text

Shared principles 1. Independent of Frameworks. 2. Testable. 3. Independent of UI. 4. Independent of Database. 5. Independent of any external agency.

Slide 12

Slide 12 text

Shared principles

Slide 13

Slide 13 text

Shared principles Ports, are contracts on the boundaries. Adapters, are implementations of the available ports to connect the app to the outside world.

Slide 14

Slide 14 text

Shared principles The outside world is anything that is not really important to solve the problem: Database, UI/HTTP/Graphql (Delivery mechanism), frameworks (Mobile, Web, … etc)

Slide 15

Slide 15 text

Shared principles

Slide 16

Slide 16 text

TDD

Slide 17

Slide 17 text

TDD traditional Cycle TDD Pyramid

Slide 18

Slide 18 text

About unit tests A test is not a unit test if: - It talks to a database - It communicates across the network - It touches the file system - You have to do things to your environment to run it (eg, change config files, comment line) - Tests that do this are integration tests.

Slide 19

Slide 19 text

About unit tests For isolation usually people talk about mocks, in reality mocks are one of the test doubles that help to to write unit tests: - Mock - Fake - Spy - Stub - Dummy

Slide 20

Slide 20 text

The approach

Slide 21

Slide 21 text

Outside-In Development with Double loop TDD

Slide 22

Slide 22 text

Outside-In Development with Double loop TDD Source

Slide 23

Slide 23 text

Outside-In Development with Double loop TDD 1. Write failing acceptance test for the application service behavior. 2. Write the application service code. 3. See the test fail. 4. Write failing microtest for domain model (core) code and see the test fail. 5. Write/design domain model code that makes the microtest pass. 6. At some point the same microtest exercise is needed to write contract tests for ports/interfaces adapters implementations . 7. Repeat 4 to 6 until the acceptance test pass.

Slide 24

Slide 24 text

Outside-In Development with Double loop TDD - Acceptance tests exercise domain model and infrastructure based on the acceptance criteria - Microtests exercise the domain model. - Contract tests exercise infrastructure ports & adapters implementations.

Slide 25

Slide 25 text

Outside-In Development with Double loop TDD

Slide 26

Slide 26 text

The problem

Slide 27

Slide 27 text

The problem Shopping cart rules: - The cart can not be orderable if it is empty - Item lines quantities are aggregated

Slide 28

Slide 28 text

The problem Shopping cart examples (acceptance criteria): - A new cart adds fried chicken, the output should say 1 fried chicken - A cart with fried chicken, it adds another fried chicken, the output should say there’s 2 fried chicken

Slide 29

Slide 29 text

Show me the code

Slide 30

Slide 30 text

References - https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architectu re.html - https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1/ - https://web.archive.org/web/20180822100852/http://alistair.cockburn.u s/Hexagonal+architecture - https://jmgarridopaz.github.io/content/hexagonalarchitecture.html - https://www.destroyallsoftware.com/screencasts/catalog/functional-cor e-imperative-shell - https://coding-is-like-cooking.info/2013/04/outside-in-development-wi th-double-loop-tdd/ - https://netflixtechblog.com/ready-for-changes-with-hexagonal-architec ture-b315ec967749 -

Slide 31

Slide 31 text

References

Slide 32

Slide 32 text

Questions?