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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
David Julia
November 20, 2014
Programming
15
0
Share
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
21
2019 Agile Australia - Strangling the Monolith
dajulia3
0
18
Event Storming Cheatsheet
dajulia3
0
270
TDD & SOLID Design Principles: Part One
dajulia3
0
22
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
21
Microservices in a Legacy Environment
dajulia3
0
15
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
20
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
24
Containers: a Basic Look Under The Hood
dajulia3
0
22
Other Decks in Programming
See All in Programming
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
350
サークル参加から学ぶ、小さな事業の回し方
yuzneri
0
180
UaaL×Androidアプリのメモリ計測 — Memory Profilerの先へ
rio432
0
160
ハーネスエンジニアリングとは?
kinopeee
13
7k
AlarmKitで明後日起きれるアラームアプリを作る
trickart
0
130
エラー処理の温故知新 / history of error handling technic
ryotanakaya
7
1.9k
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
1.2k
AgentCore Optimizationを始めよう!
licux
3
240
サプライチェーン攻撃対策「層を重ねて落ちない壁」を10日間で組み上げた話 #TechLeadConf2026
kashewnuts
1
260
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
130
AIと共に生きる技術選定 2026
sgash708
0
140
Making the RBS Parser Faster
soutaro
0
710
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Design in an AI World
tapps
1
210
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Code Reviewing Like a Champion
maltzj
528
40k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
130
Site-Speed That Sticks
csswizardry
13
1.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
170
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
A Tale of Four Properties
chriscoyier
163
24k
The Pragmatic Product Professional
lauravandoore
37
7.3k
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**