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
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
19
2019 Agile Australia - Strangling the Monolith
dajulia3
0
17
Event Storming Cheatsheet
dajulia3
0
260
TDD & SOLID Design Principles: Part One
dajulia3
0
21
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
20
Microservices in a Legacy Environment
dajulia3
0
15
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
19
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
21
Containers: a Basic Look Under The Hood
dajulia3
0
21
Other Decks in Programming
See All in Programming
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.6k
Claude Code × Gemini × Ebitengine ゲーム制作素人WebエンジニアがGoでゲームを作った話
webzawa
0
140
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
560
Agentic Elixir
whatyouhide
0
360
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
150
PHPで TLSのプロトコルを実装してみるをもう一度しゃべりたい
higaki_program
0
210
forteeの改修から振り返るPHPerKaigi 2026
muno92
PRO
3
290
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
270
ルールルルルルRubyの中身の予備知識 ── RubyKaigiの前に予習しなイカ?
ydah
1
190
実用!Hono RPC2026
yodaka
2
240
[RubyKaigi 2026] Require Hooks
palkan
1
210
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
340
Featured
See All Featured
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
770
Navigating Weather and Climate Data
rabernat
0
170
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
270
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.5k
Embracing the Ebb and Flow
colly
88
5k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.4k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
180
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
130
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.9k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
270
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
Everyday Curiosity
cassininazir
0
200
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**