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
19
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
19
Microservices in a Legacy Environment
dajulia3
0
13
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
CSC307 Lecture 08
javiergs
PRO
0
650
Vibe codingでおすすめの言語と開発手法
uyuki234
0
210
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
CSC307 Lecture 06
javiergs
PRO
0
680
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
120
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
440
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
2.4k
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
170
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
230
Fluid Templating in TYPO3 14
s2b
0
120
.NET Conf 2025 の興味のあるセッ ションを復習した / dotnet conf 2025 quick recap for backend engineer
tomohisa
0
130
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
580
Featured
See All Featured
Designing Experiences People Love
moore
144
24k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
88
Balancing Empowerment & Direction
lara
5
880
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
130
A Soul's Torment
seathinner
5
2.2k
How to train your dragon (web standard)
notwaldorf
97
6.5k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
120
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
720
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
A Tale of Four Properties
chriscoyier
162
24k
Typedesign – Prime Four
hannesfritz
42
2.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**