logo资料库

Testing Java Microservices.pdf

第1页 / 共331页
第2页 / 共331页
第3页 / 共331页
第4页 / 共331页
第5页 / 共331页
第6页 / 共331页
第7页 / 共331页
第8页 / 共331页
资料共331页,剩余部分请下载后查看
Brief Table of Contents
Chapter 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
Chapter 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
Chapter 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.2. YouTubeLink test
3.3.3. Games test
3.3.4. GamesService test
3.3.5. GamesResource test
EXERCISES
Chapter 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.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
EXERCISES
Chapter 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 PERSISTENCEEXTENSION
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
Chapter 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 GAMERAPPLICATION
6.4.1. Consumer side of the comments service
6.4.2. Provider side of the comments service
EXERCISE
Chapter 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
Chapter 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
EXERCISE
Chapter 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
Chapter 10 Continuous delivery in microservices
10.1. WHAT IS CONTINUOUS DELIVERY?
10.2. CONTINUOUS DELIVERY AND THE MICROSERVICESARCHITECTURE
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
Appendix Masking multiple containers with Arquillian Chameleon
Download from www.finelybook.com1
ayl sts Copyright story For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact torials opics fers & Deals ghlights        Special Sales Department        Manning Publications Co.        20 Baldwin Road        PO Box 761        Shelter Island, NY 11964        Email: orders@manning.com ttings ©2018 by Manning Publications Co. All rights reserved. Support Sign Out No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.  Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid­free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning 20 d al B   PO Box Shelter       R   Publications oa i n w 761 Island   NY d ,   Co. 11964     Download from www.finelybook.com2
Development editor: Cynthia Kane Technical development editor: Adam Scheller Project editor: Tiffany Taylor Copyeditor: Tiffany Taylor Proofreader: Katie Tennant Technical proofreader: Joshua White Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617292897 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – DP – 23 22 21 20 19 18 Download from www.finelybook.com3
Playlists Brief Table of Contents History Copyright Topics Brief Table of Contents Tutorials Chapter 1. An introduction to microservices Chapter 2. Application under test Chapter 3. Unit­testing microservices Chapter 4. Component­testing microservices Chapter 5. Integration­testing microservices Chapter 6. Contract tests Chapter 7. End­to­end testing Chapter 8. Docker and testing Chapter 9. Service virtualization Chapter 10. Continuous delivery in microservices  Appendix. Masking multiple containers with Arquillian Chameleon Download from www.finelybook.com4
Playlists Chapter 1. An introduction to microservices History This chapter covers Topics Why move toward a new microservice architecture? Tutorials What microservices are today, and where the future may lead Offers & Deals The basic component makeup of a microservice Testing strategies Highlights Settings Support Sign Out Traditional monolithic applications are deployed as a single package, usually as a web or enterprise­archive file (WAR or EAR). They contain all the business logic required to complete multiple tasks, often alongside the components required to render the user interface (UI, or GUI for graphical user interface). When scaling, this usually means taking a complete copy of that entire application archive onto a new server node (basically, deploying it to another server node in a cluster). It doesn’t matter where the load or bottleneck is occurring; even if it’s only in a small cross section of the application, scaling this way is an all­or­nothing approach. Microservices are specifically designed to target and change this all­or­nothing aspect by allowing you to break your business logic into smaller, more manageable elements that can be employed in multiple ways. This book isn’t intended to be a tutorial on the varied microservice architectures that are available today; we’ll assume you have some understanding of the subject. Rather, we’re going to help you overcome the challenges involved in testing the common features that all microservice applications share. In order to do that, in this chapter we’ll establish some common ground about what a microservice is, so that you can relate to where we’re coming from when we discuss these topics in later chapters. Shifting toward the ever­more­popular microservice architecture means you need to adopt new strategies in development, testing, and restructuring/refactoring and move away from some of the purely monolithic­application practices. Microservices offer you the advantage of being able to scale individual services, and the Download from www.finelybook.com5
ability to develop and maintain multiple services in parallel using several teams, but they still require a robust approach when it comes to testing. In this book, we’ll discuss various approaches for using this new, more focused way of delivering tightly packaged “micro” services and how to resolve the complex testing scenarios that are required to maintain stability across multiple teams. Later chapters will introduce an example application and how to develop testing strategies for it; this will help you better understand how to create your own test environments. You’ll see and use many features of the Arquillian test framework, which was specifically designed to tackle many of the common testing challenges you’ll face. An array of mature extensions have been developed over the years, and although other tools are available, Arquillian is our tool of choice—so expect some bias. That said, Arquillian also provides close integration with many testing tools you may already be familiar with. A note about software versions This book uses many different software packages and tools, all of which change periodically. We tried throughout the book to present examples and techniques that wouldn’t be greatly affected by these changes. All examples require Java 8, although when we finished the book, Java 10 had been released. We haven’t updated the examples because in terms of testing microservices, the release doesn’t add anything new. Something similar is true for JUnit 5. All of the examples are written using JUnit 4.12, because when we started writing the book, JUnit 5 wasn’t yet in development. At the time we finished the book, not all of the frameworks explained here have official support for JUnit 5, so we decided to skip updating the JUnit version. Other libraries, such as Spring Boot and Docker (Compose), have evolved as well during the development of the book, but none of these changes have a significant impact on how to write tests. 1.1. WHAT ARE MICROSERVICES, AND WHY USE THEM? In this section, we present what we believe is a reasonably good interpretation of the currently available answers to these questions. What you learn will provide a solid basis for understanding the microservice architecture, but expect innovation over time. We won’t make any predictions: as stated, our principle focus for the book is testing microservices, which is unlikely to change in any significant way. Download from www.finelybook.com6
It isn’t important that you fully understand the microservice architecture at this point. But if, after reading this chapter, the term microservice is still a dark void for you, we encourage you to gather more information from your own sources. Tip You may find it useful to join the open discussions at MicroProfile (http://microprofile.io). This is an initiative by the likes of IBM, London Java Community (LJC), RedHat, Tomitribe, Payara, and Hazelcast to develop a shared definition of Enterprise Java for microservices, with the goal of standardization. 1.1.1. Why use microservices? Before we delve into the nature of microservices, let’s answer the “why” question. Until recently, it’s been commonplace to develop monolithic applications, and that’s still perfectly acceptable for any application that doesn’t require scaling. The problem with scaling any kind of monolithic application is straightforward, as shown in figure 1.1. Microservices aren’t here to tell you that everything else is bad; rather, they offer an architecture that is far more resilient than a monolith to changes in the future. Figure 1.1. Scaling a monolithic application Download from www.finelybook.com7
Microservices enable you to isolate and scale smaller pieces of your application, rather than the entire application. Imagine that you’ve extracted some core business logic in your application to services A and B. Let’s say service A provides access to an inventory of items, and B provides simple statistics. You notice that on average, service A is called one million times per hour and service B is called only once per day. Scaling a monolithic application would mean adding a new node with the application that includes both services A and B. Wouldn’t it be better if you only needed to scale service A? This is where the potential of microservices becomes apparent: in the new architecture, shown in figure 1.2, services A and B become microservices A and B. You can still scale the application, but this additional flexibility is the point: you can now choose to scale where the load is greatest. Even better, you can dedicate one team of developers to maintaining microservice A and another to microservice B. You don’t need to touch the application to add features or fix bugs in either A or B, and they can also be rolled out completely independently of each other. Figure 1.2. Scaling a microservice independently of the main application Download from www.finelybook.com8
分享到:
收藏