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
13
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
16
2019 Agile Australia - Strangling the Monolith
dajulia3
0
15
Event Storming Cheatsheet
dajulia3
0
200
TDD & SOLID Design Principles: Part One
dajulia3
0
16
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
16
Microservices in a Legacy Environment
dajulia3
0
11
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
16
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
17
Containers: a Basic Look Under The Hood
dajulia3
0
17
Other Decks in Programming
See All in Programming
設計やレビューに悩んでいるPHPerに贈る、クリーンなオブジェクト設計の指針たち
panda_program
6
1.6k
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
230
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
290
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
580
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
220
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
1.1k
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
110
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
400
WebViewの現在地 - SwiftUI時代のWebKit - / The Current State Of WebView
marcy731
0
100
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
140
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
870
Deep Dive into ~/.claude/projects
hiragram
10
1.8k
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Side Projects
sachag
455
42k
Raft: Consensus for Rubyists
vanstee
140
7k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Scaling GitHub
holman
459
140k
The Cult of Friendly URLs
andyhume
79
6.5k
Code Review Best Practice
trishagee
68
18k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Rails Girls Zürich Keynote
gr2m
94
14k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
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**