Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Java Webapp Patterns: a Few Useful Tips

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

More Decks by David Julia

Other Decks in Programming

Transcript

  1. Lends itself to good architecture • MVC • Service Layer

    • DDD • Various levels of public/private • All your lovely patterns (AbstractUserDecoratorFactory) ;)
  2. Service layer = Translation layer • Into your domain terms

    • Don’t leak implementation details (result object pattern)
  3. 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(){...} } }
  4. Testing Controllers in Spring + One mockMvc test to exercise

    annotations + Others directly call method - Error handling via controller advice
  5. 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){} }
  6. When to Mock (my opinion) Services! + DB interaction +

    Complex interaction + External Services
  7. When not to mock... Arguable... + Small well-defined objects +

    individually unit tested. + No external dependencies + eg. Parsers, formatters, etc.
  8. 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**