Testing Java Microservices
Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito
Alex Soto Bueno, Andy Gumbrecht and Jason Porter
  • August 2018
  • ISBN 9781617292897
  • 296 pages
  • printed in black & white

A great and invaluable gallery of test solutions, descriptions, and examples.

Gualtiero Testa, Factor-y

Testing Java Microservices teaches you to implement unit and integration tests for microservice systems running on the JVM. You’ll work with a microservice environment built using Java EE, WildFly Swarm, and Docker. You’ll learn how to increase your test coverage and productivity, and gain confidence that your system will work as you expect.

Table of Contents detailed table of contents

preface

acknowledgments

about this book

Who should read this book

Roadmap

Code conventions and downloads

Book forum

about the authors

about the cover

1 An introduction to microservices

1.1 What are microservices, and why use them?

1.1.1 Why use microservices?

1.1.2 What are microservices?

1.1.3 Continuous integration, deployment, and Docker

1.2 Microservice networks and features

1.2.1 Microservice networks

1.2.2 Microservice features

1.3 Microservice architecture

1.3.1 Resource component

1.3.2 Business-domain component

1.3.3 Remote resources component

1.3.4 Persistence component

1.4 Microservice unit testing

1.4.1 Solitary unit tests

1.4.2 Sociable unit tests

2 Application under test

2.1 Getting started

2.2 Prerequisites

2.2.1 Java Development Kit

2.2.2 Build tools

2.2.3 Environment variables

2.2.4 Integrated development environment (IDE)

2.3 Architecture

2.3.1 The game service

2.3.2 The comments service

2.3.3 The video service

2.3.4 The aggregator service

2.3.5 Overall architecture

2.4 Application design patterns

2.4.1 Anatomy

2.4.2 ECB pattern

2.4.3 Miscellaneous patterns

2.5 Design decisions

3 Unit-testing microservices

3.1 Unit testing techniques

3.1.1 Sociable unit tests

3.1.2 Test doubles

3.1.3 Solitary unit tests

3.1.4 Unit testing in microservices

3.2 Tools

3.2.1 JUnit

3.2.2 AssertJ

3.2.3 Mockito

3.2.4 Build-script modifications

3.3 Writing unit tests for the Gamer app

3.3.1 YouTubeVideoLinkCreator test

3.3.3 Games test

3.3.4 GamesService test

3.3.5 GamesResource test

4 Component-testing microservices

4.1 The Arquillian test framework

4.2 Introducing the @RunWith(Arquillian.class) annotation

4.3 The ShrinkWrap utility class

4.3.1 Building an archive with ShrinkWrap

4.3.2 Adding content to the ShrinkWrap archive

4.3.3 Adding resources

4.3.4 Adding libraries and dependencies

4.3.5 Adding complex dependencies with the Maven resolver

4.3.6 Adding a service implementation

4.4 Write once and reuse your code

4.5 Build-script modifications

4.5.1 Defining Maven dependencies

4.5.2 Defining Gradle dependencies

4.6 Overriding the default Arquillian configuration

4.6.1 The container definition

4.6.2 Specifying container properties

4.7 Using Arquillian REST extensions

4.7.1 The Arquillian REST client extension

4.7.2 The Warp REST extension

4.8 Testing Spring applications using Arquillian

4.8.1 The Arquillian Spring Framework extension

4.8.2 Testing Spring Boot applications

4.9 More-complex Arquillian test examples

4.9.1 Testing the remote component

4.9.2 Testing the resource component

4.9.3 Testing the domain component

4.9.4 Testing the persistence component

5 Integration-testing microservices

5.1 Integration testing in the microservices architecture

5.1.1 Gateway component layer

5.1.2 Data mappers and repositories

5.2 Persistence testing with the Arquillian Persistence Extension

5.2.1 Declarative approach

5.2.2 Programmatic approach

5.2.3 Persistence testing with NoSQLUnit

5.2.4 Persistence testing with Arquillian multideployment

5.2.5 Persistence testing with Arquillian sequence

5.2.6 Build-script modifications

5.3 Writing integration tests for the Gamer application

5.3.1 Testing the Comments class

5.3.2 Testing the CommentsGateway class

5.4 Exercises

6 Contract tests

6.1 Understanding contracts

6.1.1 Contracts and monolithic applications

6.1.2 Contracts and microservice applications

6.1.3 Verifying with integration tests

6.1.4 What are contract tests?

6.1.5 Who owns the contracts?

6.2 Tools

6.2.1 Pact

6.2.2 Pact in JVM languages

6.2.3 Integrating Pact JVM into the Arquillian ecosystem with Algeron

6.3 Build-script modifications

6.3.1 Using Pact JVM for contract testing

6.3.2 Using Arquillian Algeron for contract testing

6.4 Writing consumer-driven contracts for the Gamer application

6.4.1 Consumer side of the comments service

6.4.2 Provider side of the comments service

6.5 Contract type summary

7 End-to-end testing

7.1 End-to-end tests in the overall testing picture

7.2 End-to-end testing techniques

7.2.1 Vertical tests

7.2.2 Horizontal tests

7.3 Introduction to end-to-end testing tools

7.3.1 Arquillian Cube

7.3.2 Arquillian Drone

7.3.3 Arquillian Graphene 2

7.3.4 JMeter

7.3.5 Cukes in Space

7.4 Example end-to-end test

7.4.1 Building the microservices

7.4.2 Adding the build dependencies and configuration

7.4.3 Adding @Deployment and @TargetsContainer to the test

7.4.4 Cross-origin resource sharing

7.4.5 Coping with a mixed environment using @ClassRule

7.4.6 Operating on the deployments with @OperateOnDeployment

7.4.7 Introducing @Drone, page objects, @Location, and the WebDriver

7.4.8 Working with page objects in a test

7.4.9 Running the test

7.5 Exercise

8 Docker and testing

8.1 Tools in the Docker ecosystem

8.1.1 Docker

8.1.2 Docker Machine

8.1.3 Docker Compose

8.2 Arquillian Cube

8.2.1 Setting up Arquillian Cube

8.2.2 Writing container tests

8.2.3 Writing integration tests

8.2.4 Writing end-to-end tests

8.3 Rest API

8.4 Arquillian Drone and Graphene

8.4.1 Integrating Arquillian Cube and Arquillian Drone

8.4.2 Integrating Arquillian Cube and Arquillian Graphene

8.5 Parallelizing tests

8.6 Arquillian Cube and Algeron

8.7 Using the container-objects pattern

8.7.1 Using a flexible container-object DSL

8.8 Deployment tests and Kubernetes

8.9 Build-script modifications

8.9.1 Arquillian Cube Docker

8.9.2 Arquillian Cube Docker JUnit rule

8.9.3 Arquillian Cube Kubernetes

8.9.4 Arquillian Cube OpenShift

8.10 Testing the Dockerfile for the video service

9 Service virtualization

9.1 What is service virtualization?

9.1.1 Why use service virtualization?

9.1.2 When to use service virtualization

9.2 Mimicking service responses with Hoverfly

9.2.1 Hoverfly modes

9.2.2 JUnit Hoverfly

9.2.3 Configuring Hoverfly

9.3 Build-script modifications

9.4 Using service virtualization for the Gamer application

10 Continuous delivery in microservices

10.1 What is continuous delivery?

10.2 Continuous delivery and the microservices architecture

10.3 Orchestrating continuous delivery

10.3.1 Working with Jenkins

10.3.2 The Jenkins pipeline

10.3.3 Deploying with certainty

10.4 Jenkins

10.4.1 Defining a pipeline

10.4.2 Example of a Jenkins pipeline

Appendixes

Appendix A: Masking multiple containers with Arquillian Chameleon

index

About the Technology

Microservice applications present special testing challenges. Even simple services need to handle unpredictable loads, and distributed message-based designs pose unique security and performance concerns. These challenges increase when you throw in asynchronous communication and containers.

About the book

Testing Java Microservices teaches you to implement unit and integration tests for microservice systems running on the JVM. You’ll work with a microservice environment built using Java EE, WildFly Swarm, and Docker. You’ll advance from writing simple unit tests for individual services to more-advanced practices like chaos or integration tests. As you move towards a continuous-delivery pipeline, you’ll also master live system testing using technologies like the Arquillian, Wiremock, and Mockito frameworks, along with techniques like contract testing and over-the-wire service virtualization. Master these microservice-specific practices and tools and you’ll greatly increase your test coverage and productivity, and gain confidence that your system will work as you expect.

What's inside

  • Test automation
  • Integration testing microservice systems
  • Testing container-centric systems
  • Service virtualization

About the reader

Written for Java developers familiar with Java EE, EE4J, Spring, or Spring Boot.

About the author

Alex Soto Bueno and Jason Porter are Arquillian team members. Andy Gumbrecht is an Apache TomEE developer and PMC. They all have extensive enterprise-testing experience.


buy
Testing Java Microservices (combo) added to cart
continue shopping
go to cart

combo $44.99 pBook + eBook + liveBook
Testing Java Microservices (eBook) added to cart
continue shopping
go to cart

eBook $35.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks

Covers the full spectrum of microservice-testing techniques. An indispensable book.

Piotr Gliniewicz, netPR.pl

Thorough explanations. Concrete examples. Real-world technology.

Ethan Rivett, Powerley

A beacon of light for Java developers.

Yagiz Erkan, Motive Retail