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
21
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
31
2019 Agile Australia - Strangling the Monolith
dajulia3
0
26
Event Storming Cheatsheet
dajulia3
0
290
TDD & SOLID Design Principles: Part One
dajulia3
0
29
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
22
Microservices in a Legacy Environment
dajulia3
0
25
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
28
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
26
Containers: a Basic Look Under The Hood
dajulia3
0
28
Other Decks in Programming
See All in Programming
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
750
typoなんかねぇよ
raspython3
0
670
What We Talk About When We Talk About XP
m_seki
2
390
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
Deep dive into the select statement (GopherCon UK)
jespino
0
170
ALB ログから Trace を気合で繋げる技術
fohte
7
880
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
55
36k
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
270
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
870
不幸な GC
chencmd
0
890
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
2
2k
ソフトウェアラスタライザ
fadis
1
790
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
410
Done Done
chrislema
186
16k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Being A Developer After 40
akosma
91
590k
Prompt Engineering for Job Search
mfonobong
0
440
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
470
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
440
RailsConf 2023
tenderlove
30
1.5k
Automating Front-end Workflow
addyosmani
1369
210k
The untapped power of vector embeddings
frankvandijk
2
1.9k
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**