Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Testing Spring Boot Applications
Search
Andy Wilkinson
October 09, 2019
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Testing Spring Boot Applications
Andy Wilkinson
October 09, 2019
More Decks by Andy Wilkinson
See All by Andy Wilkinson
Mastering Spring Boot's Actuator | Spring IO 2018
ankinson
4
1.5k
Documenting RESTful APIs
ankinson
4
1.2k
Documenting RESTful APIs – Webinar
ankinson
9
9.6k
Documenting RESTful APIs – Spring Exchange 2014
ankinson
4
1.3k
Other Decks in Programming
See All in Programming
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
660
数百円から始めるRuby電子工作
tarosay
0
130
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
450
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
550
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.4k
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
660
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
180
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
250
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
480
Built Our Own Background Agent at LayerX
layerx
PRO
9
4.8k
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
400
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
1
120
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
950
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Navigating Weather and Climate Data
rabernat
0
450
Building Adaptive Systems
keathley
44
3.2k
Prompt Engineering for Job Search
mfonobong
0
390
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
390
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
680
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.2k
Transcript
Testing Spring Boot Applications Andy Wilkinson @ankinson
@ankinson Why Bother Testing At All?
@ankinson
@ankinson Risk None YOLO Tests 0 Lots
@ankinson
@ankinson Unit Tests
@ankinson
@ankinson MockRestServiceServer OkHTTP’s MockWebServer Spring Data Repositories JdbcTemplate
@ankinson Testcontainers
@ankinson https://www.testcontainers.org
@ankinson @SpringBootTest @Testcontainers(disabledWithoutDocker = true) @ContextConfiguration(initializers = ExampleIntegrationTests.Initializer.class) class ExampleIntegrationTests
{ @Container public static CassandraContainer<?> cassandra = new CassandraContainer<>(); static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext context) { int cqlPort = cassandra.getMappedPort(CassandraContainer.CQL_PORT); TestPropertyValues.of("spring.data.cassandra.port=" + cqlPort) .applyTo(context.getEnvironment()); } } }
@ankinson Integration Tests
@ankinson @SpringBootTest
@ankinson Context Caching
@ankinson @SpringBootTest(properties="spring.jmx.enabled=true") @ActiveProfiles("standalone") @TestPropertySource(locations="standalone.properties") class ExampleIntegrationTests { @Test void contextLoads()
{ } } @ContextConfiguration @Import @TestPropertySource @ActiveProfiles @SpringBootTest
@ankinson @DirtiesContext
@ankinson Sliced Tests
@ankinson @JsonTest @WebMvcTest @WebFluxTest @DataJpaTest @JdbcTest @JooqTest @DataMongoTest @DataNeo4jTest @DataRedisTest
@DataLdapTest @RestClientTest
@ankinson @WebMvcTest @DataJpaTest OrderRepository extends JpaRepository<…> @Controller OrderController @Controller CustomerController
CustomerRepository extends JpaRepository<…> @SpringBootApplication Application
@ankinson @MockBean and @SpyBean
@ankinson @WebMvcTest class CustomerControllerIntegrationTests { @Autowired private MockMvc mockMvc; @Configuration
static class RepositoryConfiguration { @Bean CustomerRepository customers() { return mock(CustomerRepository.class); } @Bean OrderRepository orders() { return mock(OrderRepository.class); } } }
@ankinson Customer Customer Order @WebMvcTest class CustomerControllerTests { @Autowired private
MockMvc mockMvc; @MockBean private CustomerRepository customers; @MockBean private OrderRepository orders; } (controllers = CustomerController.class)
JUnit 5: Evolution and Innovation Sam Brannen Thursday 10:30am–11:40am Ballroom
F
Thanks! Q&A Andy Wilkinson @ankinson #springone @s1p