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
0
14
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
Tweet
Share
More Decks by David Julia
See All by David Julia
From Java to Kotlin
dajulia3
0
17
2019 Agile Australia - Strangling the Monolith
dajulia3
0
16
Event Storming Cheatsheet
dajulia3
0
240
TDD & SOLID Design Principles: Part One
dajulia3
0
18
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
19
Microservices in a Legacy Environment
dajulia3
0
12
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
18
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
19
Containers: a Basic Look Under The Hood
dajulia3
0
19
Other Decks in Programming
See All in Programming
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
490
クラウドに依存しないS3を使った開発術
simesaba80
0
180
マスタデータ問題、マイクロサービスでどう解くか
kts
0
140
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
240
Vibe codingでおすすめの言語と開発手法
uyuki234
0
130
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
140
愛される翻訳の秘訣
kishikawakatsumi
3
350
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
460
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
5
1.3k
PC-6001でPSG曲を鳴らすまでを全部NetBSD上の Makefile に押し込んでみた / osc2025hiroshima
tsutsui
0
190
これならできる!個人開発のすゝめ
tinykitten
PRO
0
130
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
410
Featured
See All Featured
Unsuck your backbone
ammeep
671
58k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
130
A Soul's Torment
seathinner
1
2k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
31
Crafting Experiences
bethany
0
22
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
150
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
110
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
120
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
200
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
32
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
0
320
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.5k
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**