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
Spring Java Webapp Patterns: a Few Useful Tips
Search
David Julia
November 20, 2014
Programming
20
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
25
2019 Agile Australia - Strangling the Monolith
dajulia3
0
25
Event Storming Cheatsheet
dajulia3
0
280
TDD & SOLID Design Principles: Part One
dajulia3
0
25
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
22
Microservices in a Legacy Environment
dajulia3
0
23
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
23
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
25
Containers: a Basic Look Under The Hood
dajulia3
0
23
Other Decks in Programming
See All in Programming
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.3k
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
210
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
130
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
730
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
800
Lessons from Spec-Driven Development
simas
PRO
0
220
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
7.4k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
3
720
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
170
Agentic UI
manfredsteyer
PRO
0
190
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
210
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
Side Projects
sachag
455
43k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Scaling GitHub
holman
464
140k
Tell your own story through comics
letsgokoyo
1
960
sira's awesome portfolio website redesign presentation
elsirapls
0
280
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Making Projects Easy
brettharned
120
6.7k
Speed Design
sergeychernyshev
33
1.9k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
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**