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
18
2019 Agile Australia - Strangling the Monolith
dajulia3
0
16
Event Storming Cheatsheet
dajulia3
0
250
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
20
Other Decks in Programming
See All in Programming
条件判定に名前、つけてますか? #phperkaigi #c
77web
2
860
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
0
180
コードレビューをしない選択 #でぃーぷらすトウキョウ
kajitack
3
1.2k
モックわからないマン卒業記 ~振る舞いを起点に見直した、フロントエンドテストにおけるモックの使いどころ~
tasukuwatanabe
3
430
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
130
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
100
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
170
[PHPerKaigi 2026]PHPerKaigi2025の企画CodeGolfが最高すぎて社内で内製して半年運営して得た内製と運営の知見
ikezoemakoto
0
310
Nuxt Server Components
wattanx
0
160
20260313 - Grafana & Friends Taipei #1 - Kubernetes v1.36 的開發雜記:那些困在 Alpha 加護病房太久的 Metrics
tico88612
0
240
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.3k
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
280
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
We Are The Robots
honzajavorek
0
200
ラッコキーワード サービス紹介資料
rakko
1
2.8M
KATA
mclloyd
PRO
35
15k
Darren the Foodie - Storyboard
khoart
PRO
3
3.1k
The SEO Collaboration Effect
kristinabergwall1
0
410
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
230
Between Models and Reality
mayunak
2
240
From π to Pie charts
rasagy
0
160
The browser strikes back
jonoalderson
0
850
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
120
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
76
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**