SlideShare a Scribd company logo
1
INTEGRATION TESTING FOR
MICROSERVICES WITH SPRING BOOT
Oleksandr Romanov
2 – 3 March 2018
Kyiv, Ukraine
2
WHAT I WILL COVER?
1) Microservices architecture and it’s testing complexities
2) Anatomy of microservice
3) Effective integration testing with Spring Boot
4) Component tests with spring-boot-test-containers
5) Conclusions
3
WHO AM I
6+ years as Test Automation Engineer
Worked with Java / C# web applications
/ backend systems from CMS to enterprise
Working now as QA Automation Lead
at Playtika
4
GOOD OLD TESTING PYRAMID
UI
SERVICE
UNIT
5
MICROSERVICES ARCHITECTURE
Microservice
Microservice
Microservice
UI
WEB
MOBILE
6
ANATOMY OF MICROSERVICE
Microservice
External
services
Persistence Domain logic
7
MICROSERVICES TEST AUTOMATION APPROACH
End – to – End (UI / API) tests
Contract
tests
Component
tests
Integration
tests
Unit tests
Component
tests
Integration
tests
Unit tests
8
WHAT IS SPRING BOOT?
https://projects.spring.io/spring-boot/
9
MACHINE MICROSERVICE
- API: GET /machine?userId={id}
{
"service": {
"code": 0
},
"data": {
"machineId": 1,
"machineAvailable": true
}
}
10
UNIT TESTS
• Verify state or behavior of unit of code within
one single microservice
• Use stubbing / mocking
Component
tests
Integration
tests
Unit tests
11
INTEGRATION TESTS
Verify service integration logic with various
external sources
- persistence: MariaDB, Couchbase,
Aerospike, Neo4j, etc)
- gateway: HTTP REST API,
Message Queues, RPC, etc.
Component
tests
Integration
tests
Unit tests
12
INTEGRATION TESTS
Spring Boot “arsenal”:
o @DataJpaTest
o @DataMongoTest
o @JdbcTest
o @DataRedisTest
o @DataNeo4jTest
Microservice
Persistence
13
ADDING CONTAINER DEPENDENCIES
https://github.com/Playtika/testcontainers-spring-boot
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-mariadb</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-couchbase</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
14
CHECK OUT APPLICATION PROPERTIES
spring.datasource.url=jdbc:mariadb://${embedded.mariadb.host}
:${embedded.mariadb.port}/${embedded.mariadb.schema}
spring.datasource.username=${embedded.mariadb.user}
spring.datasource.password=${embedded.mariadb.password}
15
EXAMPLE REPOSITORY
interface MachineSettingRepository extends
Repository<MachineSettingEntity, Long> {
Stream<MachineSettingEntity> findAll();
}
16
WRITING SIMPLE SQL DB TEST
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private MachineSettingRepository machineSettingRepository;
17
WRITING SIMPLE TEST FOR SQL DB
@Test
public void shouldFindSingleMachineSetting() throws Exception {
MachineSettingEntity settingEntity = new MachineSettingEntity(1,
“Test”);
testEntityManager.persistAndFlush(settingEntity);
assertThat(machineSettingRepository.findAll().filter(e ->
e.getMachineId() == settingEntity.getMachineId())
.collect(Collectors.toList()).get(0)).isEqualTo(settingEntity)
.withFailMessage("Unable to find machine setting with id " +
settingEntity.getMachineId());
}
18
RUNNING SQL DB TEST
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@DataJpaTest
@AutoConfigureTestDatabase(replace =
AutoConfigureTestDatabase.Replace.NONE)
@TestPropertySource(properties =
{"embedded.couchbase.enabled=false"}
public class MachineSettingDatabaseIntegrationTest
19
WRITING SIMPLE TEST FOR NOSQL DB
@Autowired
private MachineStateDocumentRepository machineStateDocumentRepository;
@Test
public void shouldFindStateInDb() {
MachineStateDocument testState = MachineStateDocument.builder()
.key("4-4")
.build();
machineStateDocumentRepository.save(testState);
assertThat(machineStateDocumentRepository.findOne("4-4").get())
.isEqualTo(testState)
.withFailMessage("Unable to find state by key: " + testState.getKey());
}
20
RUNNING NOSQL DB TEST
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = {MachineStateTestConfiguration.class})
public class MachineStateDatabaseIntegrationTest
21
ADDITIONAL CONFIGURATIONS
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
LiquibaseAutoConfiguration.class,
XADataSourceAutoConfiguration.class})
@TestConfiguration
@Profile("test")
@Import({CouchbaseConfiguration.class})
public class MachineStateTestConfiguration
22
INTEGRATION TESTS
Spring Boot “arsenal”:
o @WebMvcTest
Microservice
External
services
23
EXAMPLE CONTROLLER
@RestController
@RequestMapping("/machine")
public class MachineStateController {
private final MachineStateService machineStateService;
@GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public RestResponse<MachineState> getMachineState(@RequestParam
long userId) {
MachineState machineState = machineStateService
.getMachineState(userId);
return RestResponse.ok(machineState);
}
}
24
TESTING CONTROLLER LEVEL
private long TEST_USER_ID = 1;
@Autowired
private MockMvc mvc;
@MockBean
private MachineStateService machineStateService;
25
TESTING CONTROLLER LEVEL
@Test
public void shouldReturnMachineStateWhenGetRequestingJson() throws Exception
{
given(machineStateService.getMachineState(TEST_USER_ID))
.willReturn(new MachineState(100500, false));
mvc.perform(get("/machine?userId=1"))
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.machineId").value("100500"))
.andExpect(jsonPath("$.data.machineAvailable").value(“false"));
}
26
RUNNING TEST FOR CONTROLLER
@RunWith(SpringRunner.class)
@WebMvcTest(MachineStateController.class)
@ActiveProfiles("test")
@TestPropertySource(properties
={"embedded.couchbase.enabled=false",
"embedded.mariadb.enabled=false"})
public class MachineStateControllerIntegrationTest
27
COMPONENT TESTS
Component
tests
Integration
tests
Unit tests
External service
28
ADDING WIREMOCK DEPENDENCY
https://github.com/tomakehurst/wiremock
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>1.18</version>
<scope>test</scope>
</dependency>
29
MOCKING EXTERNAL DEPENDENCIES
{
"request":
{
"urlPattern": "/external-api/user/1/finished",
"method": "GET"
},
"response":
{
"status": 200,
"headers":
{
"Content-Type" : "application/json"
},
"body": "{"service":{"code": 0},
"data":{"finished":"true"}}"
}
}
30
SETTING UP COMPONENT TEST
public abstract class MachineBaseComponentTest {
@ClassRule
public static final WireMockClassRule externalApi = new
WireMockClassRule(7070);
@Autowired
protected WebApplicationContext context;
protected MockMvc mvc;
@Before
public void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
}
31
COMPONENT TEST
@Test
public void shouldReturnMachineStateWhenStateIsInRepository() throws
Exception {
mvc.perform(get("/machine?userId=1"))
.andExpect(status().is(200))
.andExpect(jsonPath("$.data.machineId").value("1"))
.andExpect(jsonPath("$.data.machineAvailable").value("true"));
}
32
RUNNING COMPONENT TESTS
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MachineApplication.class,
MachineStateComponentTests.MachineTestConfiguration.class})
@ActiveProfiles("test")
public abstract class MachineBaseComponentTest {}
33
CONCLUSIONS
Microservices needs an
additional levels of testing
Component
tests
Integration
tests
Unit tests
34
CONCLUSIONS
Integration testing with containers provides:
• Better reliability
• More configurability
• Speed of test execution
35© 2017 Playtika Ltd. All Rights Reserved
THANK YOU!
Oleksandr Romanov
twitter: @al8xr
mail: al8x.romanov@gmail.com

More Related Content

What's hot (20)

PPTX
20180518 QNAP Seminar - Introduction to React Native
Eric Deng
 
PPTX
Hexagonal architecture with Spring Boot
Mikalai Alimenkou
 
PDF
Introducao eng software [modo de compatibilidade]
Fernanda Ramos
 
PDF
User experience. What is it anyway?
Marilyn Langfeld
 
PDF
50.000 orange stickies later
Alberto Brandolini
 
PPTX
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
PPTX
Webinar presentation on cloud computing
Folasade Adedeji
 
PDF
Amazon EKS를 통한 빠르고 편리한 컨테이너 플랫폼 활용 – 이일구 AWS 솔루션즈 아키텍트:: AWS Cloud Week - Ind...
Amazon Web Services Korea
 
PPT
SOLID Design Principles
Andreas Enbohm
 
PPTX
Zuul @ Netflix SpringOne Platform
Mikey Cohen - Hiring Amazing Engineers
 
PPT
User Research 101
Christina Wodtke
 
PPT
Introduction to Cloud Computing
Tom Eberle
 
PPTX
Understand AWS Pricing
Lynn Langit
 
PPTX
Introduction to Node js
Akshay Mathur
 
PDF
Web Development with HTML5, CSS3 & JavaScript
Edureka!
 
PPTX
Descubriendo windows azure
Javier Suárez Ruiz
 
PDF
React JS - Introduction
Sergey Romaneko
 
PDF
Oracle CodeOne 2019: Descending the Testing Pyramid: Effective Testing Strate...
Chris Richardson
 
PDF
Get the Look and Feel You Want in Oracle APEX
Jorge Rimblas
 
PDF
Infographic: AWS vs Azure vs GCP: What's the best cloud platform for enterprise?
Veritis Group, Inc
 
20180518 QNAP Seminar - Introduction to React Native
Eric Deng
 
Hexagonal architecture with Spring Boot
Mikalai Alimenkou
 
Introducao eng software [modo de compatibilidade]
Fernanda Ramos
 
User experience. What is it anyway?
Marilyn Langfeld
 
50.000 orange stickies later
Alberto Brandolini
 
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
Webinar presentation on cloud computing
Folasade Adedeji
 
Amazon EKS를 통한 빠르고 편리한 컨테이너 플랫폼 활용 – 이일구 AWS 솔루션즈 아키텍트:: AWS Cloud Week - Ind...
Amazon Web Services Korea
 
SOLID Design Principles
Andreas Enbohm
 
Zuul @ Netflix SpringOne Platform
Mikey Cohen - Hiring Amazing Engineers
 
User Research 101
Christina Wodtke
 
Introduction to Cloud Computing
Tom Eberle
 
Understand AWS Pricing
Lynn Langit
 
Introduction to Node js
Akshay Mathur
 
Web Development with HTML5, CSS3 & JavaScript
Edureka!
 
Descubriendo windows azure
Javier Suárez Ruiz
 
React JS - Introduction
Sergey Romaneko
 
Oracle CodeOne 2019: Descending the Testing Pyramid: Effective Testing Strate...
Chris Richardson
 
Get the Look and Feel You Want in Oracle APEX
Jorge Rimblas
 
Infographic: AWS vs Azure vs GCP: What's the best cloud platform for enterprise?
Veritis Group, Inc
 

Similar to Integration testing for microservices with Spring Boot (20)

PPTX
Microservice test strategies for applications based on Spring, K8s and Istio
Ahmed Misbah
 
PPTX
Easy Java Integration Testing with Testcontainers​
Payara
 
PDF
Pragmatic Java Test Automation
Dmitry Buzdin
 
PPTX
Introduction to Spring Boot
Purbarun Chakrabarti
 
PDF
GeeCON 2017 - TestContainers. Integration testing without the hassle
Anton Arhipov
 
PDF
Testing your application on Google App Engine
Inphina Technologies
 
PDF
Testing Your Application On Google App Engine
IndicThreads
 
PDF
Testing with Spring 4.x
Sam Brannen
 
PPTX
GeeCon.cz - Integration Testing from the Trenches Rebooted
Nicolas Fränkel
 
PDF
Testing with Spring: An Introduction
Sam Brannen
 
PDF
SpringOne 2GX 2013 - Spring Testing
Mattias Severson
 
PDF
GeeCON 2014 - Spring Testing
Mattias Severson
 
PDF
Test Pyramid in Microservices Context
Dhaval Dalal
 
PPTX
JavaLand - Integration Testing How-to
Nicolas Fränkel
 
PPTX
201502 - Integration Testing
lyonjug
 
PDF
Level Up Your Integration Testing With Testcontainers
VMware Tanzu
 
PDF
Integration testing - A&BP CC
JWORKS powered by Ordina
 
PPT
Spring Boot in Action
Alex Movila
 
PDF
Spring 3.1 and MVC Testing Support
Sam Brannen
 
PDF
JUnit5 and TestContainers
Sunghyouk Bae
 
Microservice test strategies for applications based on Spring, K8s and Istio
Ahmed Misbah
 
Easy Java Integration Testing with Testcontainers​
Payara
 
Pragmatic Java Test Automation
Dmitry Buzdin
 
Introduction to Spring Boot
Purbarun Chakrabarti
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
Anton Arhipov
 
Testing your application on Google App Engine
Inphina Technologies
 
Testing Your Application On Google App Engine
IndicThreads
 
Testing with Spring 4.x
Sam Brannen
 
GeeCon.cz - Integration Testing from the Trenches Rebooted
Nicolas Fränkel
 
Testing with Spring: An Introduction
Sam Brannen
 
SpringOne 2GX 2013 - Spring Testing
Mattias Severson
 
GeeCON 2014 - Spring Testing
Mattias Severson
 
Test Pyramid in Microservices Context
Dhaval Dalal
 
JavaLand - Integration Testing How-to
Nicolas Fränkel
 
201502 - Integration Testing
lyonjug
 
Level Up Your Integration Testing With Testcontainers
VMware Tanzu
 
Integration testing - A&BP CC
JWORKS powered by Ordina
 
Spring Boot in Action
Alex Movila
 
Spring 3.1 and MVC Testing Support
Sam Brannen
 
JUnit5 and TestContainers
Sunghyouk Bae
 
Ad

More from Oleksandr Romanov (10)

PPTX
Тестування Blockchain - Що там можна тестувати?
Oleksandr Romanov
 
PDF
What does it mean to test a blockchain
Oleksandr Romanov
 
PPTX
Ups and downs of contract testing in real life
Oleksandr Romanov
 
PPTX
Testing challenges at microservices world
Oleksandr Romanov
 
PPTX
Practical contract testing with Spring Cloud Contract [Test Con 2019]
Oleksandr Romanov
 
PPTX
Turning automation education upside down [QAFest 2019]
Oleksandr Romanov
 
PPTX
Hidden complexities in microservices testing
Oleksandr Romanov
 
PPTX
Automating microservices: what, where and when
Oleksandr Romanov
 
PDF
Introduction to web application security testing
Oleksandr Romanov
 
PDF
Introduction to pairwise testing
Oleksandr Romanov
 
Тестування Blockchain - Що там можна тестувати?
Oleksandr Romanov
 
What does it mean to test a blockchain
Oleksandr Romanov
 
Ups and downs of contract testing in real life
Oleksandr Romanov
 
Testing challenges at microservices world
Oleksandr Romanov
 
Practical contract testing with Spring Cloud Contract [Test Con 2019]
Oleksandr Romanov
 
Turning automation education upside down [QAFest 2019]
Oleksandr Romanov
 
Hidden complexities in microservices testing
Oleksandr Romanov
 
Automating microservices: what, where and when
Oleksandr Romanov
 
Introduction to web application security testing
Oleksandr Romanov
 
Introduction to pairwise testing
Oleksandr Romanov
 
Ad

Recently uploaded (20)

PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PDF
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPT
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
PDF
POWER PLANT ENGINEERING (R17A0326).pdf..
haneefachosa123
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
PPTX
Structural Functiona theory this important for the theorist
cagumaydanny26
 
PDF
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
POWER PLANT ENGINEERING (R17A0326).pdf..
haneefachosa123
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
Structural Functiona theory this important for the theorist
cagumaydanny26
 
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 

Integration testing for microservices with Spring Boot