Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Spring Java Webapp Patterns: a Few Useful Tips
Search
David Julia
November 20, 2014
Programming
22
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Spring Java Webapp Patterns: a Few Useful Tips
A few quick tips on what I found useful for testing/app architecture tips
David Julia
November 20, 2014
More Decks by David Julia
See All by David Julia
From Java to Kotlin
dajulia3
0
36
2019 Agile Australia - Strangling the Monolith
dajulia3
0
30
Event Storming Cheatsheet
dajulia3
0
300
TDD & SOLID Design Principles: Part One
dajulia3
0
39
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
25
Microservices in a Legacy Environment
dajulia3
0
33
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
31
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
30
Containers: a Basic Look Under The Hood
dajulia3
0
32
Other Decks in Programming
See All in Programming
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
600
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.2k
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
残高管理から台帳サービスへの進化
artoy
1
200
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
540
AmebaブログはKMPから卒業します
kmars0605
0
190
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
golang-bob.pdf
iwatsukayura
0
130
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
0
1.4k
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
350
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
1
440
Featured
See All Featured
WENDY [Excerpt]
tessaabrams
14
40k
Designing Powerful Visuals for Engaging Learning
tmiket
1
610
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
So, you think you're a good person
axbom
PRO
2
2.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Faster Mobile Websites
deanohume
310
32k
Marketing to machines
jonoalderson
1
5.9k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
710
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Designing for Timeless Needs
cassininazir
1
530
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Transcript
Spring Java Patterns A few things I’ve found useful when
building web apps recently
Lends itself to good architecture • MVC • Service Layer
• DDD • Various levels of public/private • All your lovely patterns (AbstractUserDecoratorFactory) ;)
Service layer = Translation layer • Into your domain terms
• Don’t leak implementation details (result object pattern)
Service With Result Object public class OrderService { public OrderCancellationResult
cancelOrder(){ ... return new OrderCancellationResult(status, referenceNumber); } class OrderCancellationResult{ public OrderCancellationResult( String status, String ReferenceNumber){...}; public String getStatus(){...}; //Pending, Rejected, Awaiting Review public String getTransactionReferenceNumber(){...} } }
Doesn’t Leak HTTP status codes!
Testing Controllers in Spring + One mockMvc test to exercise
annotations + Others directly call method - Error handling via controller advice
Controller Testing Testing through http avoids brittle tests, allows refactoring
@Test public void getAccount() throws Exception { when(userService.findUser(anyInt())).thenReturn("element"); this.mockMvc.perform(get("/users/123") andExpect(status().isOk()); } @Test public void getAccount_Happy() throws Exception { when(userService.findUser(anyInt())) .thenReturn(new User("jim"); User result = controller.readUser(1238439) assertThat(result).Equals(new User("jim"))) } @Test(expected=RecordNotFound.class) public void getAccount_NotFound() throws Exception { when(userService.findUser(anyInt())).thenReturn(null); controller.readUser(1238439) } class GlobalControllerExceptionHandler { @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(RecordNotFound.class) public void handleNotFound() { return new ErrorResponse("Not Found"); } } @RestController class UserController{ @RequestMapping(value ="/users/{userId}", method = RequestMethod.GET) public Account getAccount(@PathVariable Long userId){} }
When to Mock (my opinion) Services! + DB interaction +
Complex interaction + External Services
When not to mock... Arguable... + Small well-defined objects +
individually unit tested. + No external dependencies + eg. Parsers, formatters, etc.
Use Judiciously • Static Imports (especially with hamcrest/Mockito) • Heavily
configured MockMVC (eg mockFilterChain) • Integration Tests Testing from inside a package (Legacy Code) **point of contention**