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
7
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
10
2019 Agile Australia - Strangling the Monolith
dajulia3
0
10
Event Storming Cheatsheet
dajulia3
0
94
TDD & SOLID Design Principles: Part One
dajulia3
0
9
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
8
Microservices in a Legacy Environment
dajulia3
0
6
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
8
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
10
Containers: a Basic Look Under The Hood
dajulia3
0
10
Other Decks in Programming
See All in Programming
Jetpack Composeとデザインシステム
rmakiyama
0
230
Documentation testsの恩恵 / Documentation testing benefits
ssssota
1
560
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
74k
Criando a Woovi em uma semana
daniloab
0
120
Open AI APIを使う前に知っておきたいアカウントTier の話
akki_megane
0
130
Direct Style Effect Systems The Print[A] ExampleA Comprehension Aid
philipschwarz
PRO
0
410
Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | AndroidMakers Paris
prof18
0
180
ServerAction で Progressive Enhancement はどこまで頑張れるか? / progressive-enhancement-with-server-action
takefumiyoshii
6
510
『WordPressコミュニティで学ぶ』OSS貢献の多様性
ippey
0
230
TypeScriptのパフォーマンス改善
yajihum
14
5k
Exploring Type-Informed Lint Rules in Rust based TypeScript Linters
unvalley
3
630
Powerfully Typed TypeScript
euxn23
3
1.6k
Featured
See All Featured
Ruby is Unlike a Banana
tanoku
96
10k
Unsuck your backbone
ammeep
664
57k
Embracing the Ebb and Flow
colly
80
4.2k
Statistics for Hackers
jakevdp
790
220k
The Illustrated Children's Guide to Kubernetes
chrisshort
32
47k
A Tale of Four Properties
chriscoyier
153
22k
Build your cross-platform service in a week with App Engine
jlugia
226
17k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
67
14k
The Cost Of JavaScript in 2023
addyosmani
21
4k
Building a Scalable Design System with Sketch
lauravandoore
457
32k
Optimising Largest Contentful Paint
csswizardry
13
2.4k
Designing the Hi-DPI Web
ddemaree
276
33k
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**