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
220
TDD & SOLID Design Principles: Part One
dajulia3
0
18
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
18
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
18
Containers: a Basic Look Under The Hood
dajulia3
0
18
Other Decks in Programming
See All in Programming
Leading Effective Engineering Teams in the AI Era
addyosmani
3
350
明日から始めるリファクタリング
ryounasso
0
140
なぜあの開発者はDevRelに伴走し続けるのか / Why Does That Developer Keep Running Alongside DevRel?
nrslib
3
400
CSC509 Lecture 06
javiergs
PRO
0
260
その面倒な作業、「Dart」にやらせませんか? Flutter開発者のための業務効率化
yordgenome03
1
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
300
私達はmodernize packageに夢を見るか feat. go/analysis, go/ast / Go Conference 2025
kaorumuta
2
550
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
10
6.7k
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
2
840
大規模アプリのDIフレームワーク刷新戦略 ~過去最大規模の並行開発を止めずにアプリ全体に導入するまで~
mot_techtalk
1
440
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
180
Cloudflare AgentsとAI SDKでAIエージェントを作ってみた
briete
0
140
Featured
See All Featured
Become a Pro
speakerdeck
PRO
29
5.5k
Building Applications with DynamoDB
mza
96
6.7k
Into the Great Unknown - MozCon
thekraken
40
2.1k
Building Adaptive Systems
keathley
43
2.8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
61k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Optimizing for Happiness
mojombo
379
70k
The Straight Up "How To Draw Better" Workshop
denniskardys
238
140k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
870
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.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**