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
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
510
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
550
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
200
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
180
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
160
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
680
組織で育むオブザーバビリティ
ryota_hnk
0
170
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.5k
dchart: charts from deck markup
ajstarks
3
990
Fragmented Architectures
denyspoltorak
0
150
Featured
See All Featured
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
280
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
GraphQLとの向き合い方2022年版
quramy
50
14k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Writing Fast Ruby
sferik
630
62k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
0
100
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
RailsConf 2023
tenderlove
30
1.3k
Darren the Foodie - Storyboard
khoart
PRO
2
2.3k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
300
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
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**