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
21
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
27
2019 Agile Australia - Strangling the Monolith
dajulia3
0
25
Event Storming Cheatsheet
dajulia3
0
280
TDD & SOLID Design Principles: Part One
dajulia3
0
28
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
22
Microservices in a Legacy Environment
dajulia3
0
24
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
24
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
25
Containers: a Basic Look Under The Hood
dajulia3
0
25
Other Decks in Programming
See All in Programming
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
580
作るコストが小さくなった時代 幸せに働くために改めて考えたいこと 〜エンジニアとして価値を出し続けるために注視している二分野〜
yuppeeng
0
180
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
320
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
160
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
500
その節約、円になってますか?
isamumumu
1
650
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
430
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.5k
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.6k
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.7k
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
620
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
200
Featured
See All Featured
Paper Plane
katiecoart
PRO
2
52k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
200
Music & Morning Musume
bryan
47
7.3k
Odyssey Design
rkendrick25
PRO
2
740
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Practical Orchestrator
shlominoach
191
12k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
790
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
230
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
A Soul's Torment
seathinner
6
3.1k
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**