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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
David Julia
November 20, 2014
Programming
16
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
22
2019 Agile Australia - Strangling the Monolith
dajulia3
0
20
Event Storming Cheatsheet
dajulia3
0
270
TDD & SOLID Design Principles: Part One
dajulia3
0
24
Pragmatic Kotlin Libraries: Containing Platform Types
dajulia3
0
21
Microservices in a Legacy Environment
dajulia3
0
19
Unlocking Features with Microservices in a Legacy Environment
dajulia3
0
22
Introducing Lattice: an App-Centric Container Orchestration Platform
dajulia3
0
24
Containers: a Basic Look Under The Hood
dajulia3
0
22
Other Decks in Programming
See All in Programming
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3k
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
320
ReactとSvelteのその先、Ripple-TS / Beyond React and Svelte: Ripple-TS
ssssota
3
1.9k
OCRを使ってゲームのアイテムをデータ化する
kishikawakatsumi
0
130
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.3k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
240
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
2
390
AI時代のUIはどこへ行く?その2!
yusukebe
19
6.3k
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
260
AIとRubyの静的型付け
ukin0k0
0
510
Oxlintのカスタムルールの現況
syumai
5
970
TAKTでAI駆動開発の品質を設計する
j5ik2o
2
140
Featured
See All Featured
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
400
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
240
GraphQLとの向き合い方2022年版
quramy
50
15k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
150
RailsConf 2023
tenderlove
30
1.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
860
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
How to Think Like a Performance Engineer
csswizardry
28
2.6k
Docker and Python
trallard
47
3.9k
Deep Space Network (abreviated)
tonyrice
0
160
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
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**