SlideShare a Scribd company logo
Introduction to dependency injection.
Spring, AngularJs
Dependency
Injection
Brief introduction
0A pre-dependency injection
Base example
Advantages
• easily enable testability
• clean and concise code
• testing client and service in isolation
• client no need to know how to build the dependency object graph
0A pre-dependency injection
Encapsulate creation
1 public class Emailer {	
2 	
3 private SpellChecker spellChecker;	
4 	
5 public Emailer() {	
6 this.spellChecker = new SpellChecker();	
7 }	
8 	
9 public void send(String text) {	
10 //some implementation	
11 }	
12 }
How to test?
• Impossible: MockSpellChecker cannot substitute the internal spellChecker.
Change implementation?
• Impossible: Emailer encapsulates the creation of its dependencies.
0A pre-dependency injection
Construction by hand
1 public class Emailer {	
2 	
3 private SpellChecker spellChecker;	
4 	
5 public void send(String text) {	
6 //some implementation	
7 }	
8 	
9 public void setSpellChecker(SpellChecker spellChecker) {	
10 this.spellChecker = spellChecker;	
11 }	
12 }
Pros
• Setter method: different flavors of the service can be injected.
Cons
• Object construction knowledge: on the client of the service.
• Repeat wiring code
• What about: encapsulation principle and altering the object graph?
0A pre-dependency injection
Construction by hand test
Pros
• Setter method: MockSpellChecker can be easily injected.
• Check if dependency has been correctly used.
Cons
• Repeat wiring code
1 @Test	
2 public void testEmailerCheckSpelling() throws Exception {	
3 	
4 MockSpellChecker mockSpellChecker = new MockSpellChecker();	
5 	
6 Emailer emailer = new Emailer();	
7 	
8 emailer.setSpellChecker(mockSpellChecker);	
9 emailer.send("Hello World");	
10 	
11 assert mock.isSpellCheckingDone();	
12 }
0A pre-dependency injection
Factory pattern
Pros
• Level of abstraction: Emailer client code is separated from the Emailer creation.
• No internal knowledge: client code only use a specific factory to obtain a dependency.
• Code clean and concise even with richer and more complex object graph.
Cons
• Extreme growth: factory classes tend to grow becoming really big and uncontrolled.
1 public class EmailerFactory {	
2 	
3 public Emailer newFrenchEmailer() {	
4 return new Emailer(new FrenchSpellChecker());	
5 }	
6 	
7 public Emailer newJapaneseEmailer() {	
8 return new Emailer(new JapaneseSpellChecker());	
9 }	
10 }	
11 	
12 //somewhere in the Client code	
13 Emailer emailer = new EmailerFactory().newFrenchEmailer();
0A pre-dependency injection
Factory pattern test
Pros
• Level of abstraction: Emailer client code is separated from the Emailer creation.
• No internal knowledge: client code only use a specific factory to obtain a dependency.
• Code clean and concise even with richer and more complex object graph.
Cons
• Extreme growth and maintenance: factory classes tend to grow becoming really big and uncontrolled.
1 public class EmailerFactory {	
2 	
3 public Emailer newFrenchEmailer() {	
4 return new Emailer(new FrenchSpellChecker());	
5 }	
6 	
7 public Emailer newJapaneseEmailer() {	
8 return new Emailer(new JapaneseSpellChecker());	
9 }	
10 }	
11 	
12 //somewhere in the Client code	
13 Emailer emailer = new EmailerFactory().newFrenchEmailer();
0A pre-dependency injection
Factory pattern test
1 public class EmailClient {	
2 private Emailer emailer = new EmailerFactory.newEnglishEmailer();	
3 	
4 public void run() {	
5 emailer.send("Hello World");	
6 }	
7 }	
8 	
9 @Test	
10 public void testEmailClient() {	
11 MockEmailer mock = new MockEmailer();	
12 EmailerFactory.set(mock);	
13 	
14 new EmailClient.run();	
15 	
16 assert mock.correctlySent();	
17 }
How & Cons to test with factory
• Heavily modify factory class code to support testing for any client, state is shared thus
code cleanup required after each test.
• Create a factory mock or stub which can easily grow, it must accompany every variation
of every service.
0B dependency injection
Hollywood Principle: don’t call us; we’ll call you!
Enable
• Unawareness: client not aware of the Emailer kind.
• Reversal of responsibilities: service not explicitly required.
Pros
• Easily enable testability via setter method or constructor injection (construction by hand).
• Client no needs to know its dependencies and how to build them (factory pattern).
• Dependencies are not required but provided (new).
1 public class Emailer {	
2 public void send(String text) {	
3 //some implementation	
4 }	
5 //some other methods	
6 }	
7 	
8 public class SimpleEmailClient {	
9 private Emailer emailer;	
10 	
11 public SimpleEmailClient(Emailer emailer) {	
12 this.emailer = emailer;	
13 }	
14 public void sendEmail() {	
15 emailer.send(readMessage());	
16 }	
17 }
0B dependency injection
Hollywood Principle: don’t call us; we’ll call you!
embodies the
Hollywood Principle
Dependency Injector
• Control over construction, wiring and assembly dependency or object graph.
• Separates infrastructure code (wiring and construction) from application code (core
purpose of the service)
0C dependency injection with Spring
Spring Framework
Spring
• Open source application framework and inversion of control container for Java platform.
• 1 October 2002, release 1.0.0, 7 July 2014, release 4.0.6, now 4.1.0 RC1
• active development and evolution to meet new programming requirements
• Developed by Pivotal, a spin-out and joint venture of EMC Corporation and VMware.
• Excellent documentation, clean and tested code, wide community and support.
Projects
• Spring Framework
• core support for dependency injection, transaction management
• Spring Data
• provide consistent approach to data access
• relational, non-relational, map-reduce and much more
• Spring XD
• simplify the development of big data applications
• address analytics, batch jobs and data export
• much more…
0C dependency injection with Spring
Example: the client of the service
Client
• SimpleEmailClient decoupled from the EmailerService implementation.
• SimpleEmailClient unaware of which implementation will be provided at runtime,
programming against interfaces (OOP principle).
1 @Component	
2 public class SimpleEmailClient {	
3 	
4 final private EmailerService service;	
5 	
6 @Autowired	
7 public SimpleEmailClient(EmailerService service) {	
8 this.service = service;	
9 }	
10 	
11 public void printMessage() {	
12 System.out.println(this.service.getMessage());	
13 }	
14 }
1 @Configuration	
2 @ComponentScan	
3 public class Application {	
4 	
5 @Bean	
6 MessageService mockEmailerService() {	
7 return new EmailerService() {	
8 public String getMessage() {	
9 return "Hello World!";	
10 }	
11 };	
12 }	
13 	
14 public static void main(String[] args) {	
15 ApplicationContext context =	
16 new AnnotationConfigApplicationContext(Application.class);	
17 SimpleEmailClient client = context.getBean(SimpleEmailClient.class);	
18 client.printMessage();	
19 }	
20 }	
0C dependency injection with Spring
Example: the service and the mock
Injected mock
implements the
interface
Injector wires client
with its dependency
0C dependency injection with Spring
Another example with integration test
1 @Entity	
2 @Table(name = "MOVIE")	
3 public class Movie extends AbstractEntity {	
4 	
5 @Column(name = "TITLE")	
6 private String title;	
7 	
8 protected Movie() {...}	
9 }
Movie example
• Simple Movie class annotated JPA 2.0, Hibernate, store and retrieve.
• A service interface is given.
• Repository and service should be created and implemented to support basic CRUD operations.
Code on Github
• The code of the example is available on Github
1 public interface MovieService {	
2 	
3 List<Movie> findAll();	
4 	
5 Movie create(final Movie movie);	
6 	
7 Movie delete(final Movie movie);	
8 	
9 Movie update(final Movie movie);	
10 }
0C dependency injection with Spring
Integration test: another example
Movie repository and service
• No need to implement basic CRUD methods as findAll(), delete(), save() etc.
• Methods are resolved based on attribute names, just declare in the repository interface findById() to have
it automatically implemented at runtime where Id is the attribute.
• More complex methods have to be directly implemented.
• Clean and concise code, no need to implement explicit SQL query, reduce boiler-plate code.
1 @Repository	
2 @Transactional	
3 public class MovieServiceImpl implements MovieService {	
4 	
5 @Autowired	
6 private MovieRepository movieRepository;	
7 	
8 @Override	
9 public final List<Movie> findAll() {	
10 return Lists.newArrayList(movieRepository.findAll());	
11 }	
12 ...	
13 }
1 public interface MovieRepository extends CrudRepository<Movie, Long> {	
2 }
0C dependency injection with Spring
Integration test: another example
1 @ActiveProfiles("test")	
2 public class MovieServiceImplTest extends AbstractServiceImplTest {	
3 	
4 	
5 @Autowired	
6 private MovieService movieService;	
7 	
8 @DataSets(setUpDataSet = "/MovieServiceImplTest.xls")	
9 @Test	
10 public final void testFindAll() throws Exception {	
11 List<Movie> results = movieService.findAll();	
12 	
13 assertNotNull(results);	
14 assertThat(results.size(), equalTo(3));	
15 }	
16 }	
Injected
implementation based
on the interface
Build ApplicationContext
based on the active
profile
Insert into the data source tables
from xls file before the method
execution.
After method execution
automatic rollback.
1 @RunWith(SpringJUnit4ClassRunner.class)	
2 @ContextConfiguration(classes = {ServiceTestConfig.class})	
3 @TestExecutionListeners({ServiceTestExecutionListener.class})	
4 @ActiveProfiles("test")	
5 public abstract class AbstractServiceImplTest extends 	
	 	 	 	 	 	 AbstractTransactionalJUnit4SpringContextTests {	
6 	
7 @PersistenceContext	
8 protected EntityManager em;	
9 }	
0C dependency injection with Spring
ApplicationContext
configuration with a
specific data source,
schema and data.
Based on a standard
JUnit Runner.
Standard javax.persistence,
hibernate jpa 2.0 api.
Listener to intercept
standard test execution
lifecycle.
Test
• Non invasive solution to build integration tests based on standard frameworks like
JUnit and Hibernate
• It works on top of standard frameworks providing clean and concise code for complex
test cases.
Integration test: another example
1 @Configuration	
2 @ImportResource("classpath:datasource-tx-jpa.xml")	
3 @ComponentScan(basePackages = {"com.contrastofbeauty.movieapp.service"})	
4 @Profile("test")	
5 public class ServiceTestConfig {	
6 	
7 @Bean	
8 public DataSource dataSource() {	
9 return new EmbeddedDatabaseBuilder()	
	 	 	 	 	 	 	 .setType(EmbeddedDatabaseType.H2)	
	 	 	 	 	 	 	 .addScript("classpath:schema.sql").build();	
10 }	
11 	
12 @Bean(name = "databaseTester")	
13 public DataSourceDatabaseTester dataSourceDatabaseTester() {	
14 DataSourceDatabaseTester databaseTester = new DataSourceDatabaseTester(dataSource());	
15 return databaseTester;	
16 }	
17 	
18 @Bean(name = "xlsDataFileLoader")	
19 public XlsDataFileLoader xlsDataFileLoader() {	
20 return new XlsDataFileLoader();	
21 }	
22 }	
0C dependency injection with Spring Reuse transaction and
JPA configuration
DbUnit classes to load xls files
and setup the data source
Data source
configuration
Integration test: another example
0C dependency injection with Spring
Considerations
Spring Debate
• Spring is not standard.
• Just pointless, Spring promote the use of standard.
• Really important to have a standard, but if the solution of your problem is not standard you will
wait three or more years?
• Spring Social (JSR-357 rejected in 2012, really opened debate on future)
• Widespread container testing done with Arquillan, JBoss specific.
• Spring is not compatible.
• Free to use JPA, CDI annotations etc.
• Use all Java EE technologies and additional features provide by Spring.
• Spring works with all Java EE application servers and more like Jetty and Tomcat.
• Spring is a must, best choice
• Not always and Just pointless, what are your requirements?
• Spring is a meta-framework, highly scalable, highly configurable but complex.
• Spring configuration could be difficult to manage when the project grows.
• Use only Spring dependency injection? May be consider what already provided by the container.
0D AngularJS
$scope, $controller, $http and test with mocks
Where to find the code
• Github for source code: angulajs-popcorn
• Plunker online live editing: application and testing
0D AngularJS
Definition
MVC Design Pattern
• View is projection of the model through the HTML template.
• Controller is a Javascript constructor function used to augment the Angular scope.
• Scope an object referring to application model, used along with data model, template and controller to keep
models and views in sync.
Template Model View
(model/view data binding)
Root
Scope
movieController
Scope
movies: Array
<!DOCTYPE html>
<html lang="en" ng-app="movieApp">
<body ng-controller="movieController">
<ul>
<li ng-repeat="movie in movies">
{{movie.title}}
</li>
</ul>
</body>
</html>
Repetear
Scope
movie: Object
▪ Commando
▪ Raw Deal
▪ Predator
n-times
ng-controller
ng-app
n-times
0D AngularJS dependency injection
Definition
Angular Injection
• $scope injected into controller, scope is the glue between application controller and the view.
• $http injected, an Angular service to facilitate the communication with the remote HTTP server.
• $httpBackend mock of the real $httpBackend, backend used by the service to delegate to XMLHttpRequest.
1 var movieApp = angular.module('movieApp', ['xeditable', 'ngMockE2E']);	
2 	
3 movieApp.controller('MovieController', function($scope, $http) {	
4 	
5 $scope.loadMovies = function() {	
6 return $http.get("/movies").success(function(response){	
7 $scope.movies = response;	
8 });	
9 };	
10 });	
11 	
12 // --------------- mock $http requests ----------------------	
13 movieApp.run(function($httpBackend) {	
14 $httpBackend.whenGET('/movies').respond([	
15 {id: 1, title: "Commando", actors: "Arnold Schwarzenegger"},	
16 {id: 2, title: "Raw Deal", actors: "Arnold Schwarzenegger"},	
17 {id: 3, title: "Predator", actors: "Arnold Schwarzenegger"}	
18 ])	
19 });
0D AngularJS dependency injection
Test controller using Jasmine BDD framework
1 describe('Movie Controller test suite', function() {	
2 	
3 describe('MovieController', function() {	
4 	
5 var $scope, $httpBackend, createController, $http;	
6 	
7 beforeEach(module('movieApp'));	
8 	
9 beforeEach(inject(function($controller, $rootScope, _$httpBackend_, _$http_) {	
10 $scope = $rootScope.$new();	
11 $httpBackend = _$httpBackend_;	
12 $http = _$http_;	
13 	
14 createController = function() {	
15 return $controller('MovieController', {	
16 $scope: $scope	
17 });	
18 };	
19 }));	
20 	
21 it("should GET all the movies", function() {	
22 $httpBackend.expectGET('/movies').respond(200, [{}, {}, {}]);	
23 createController();	
24 $scope.loadMovies();	
25 $httpBackend.flush();	
26 expect($scope.movies.length).toBe(3);	
27 });	
28 });
Jasmine description suite
Setup test context injecting
Angular services
Jasmine test declaration
or spec
Obtain the controller and
invoke the function to test
Assert the result
0D AngularJS
Considerations
Advantages
• clean separation of concerns
• template is pure HTML, no other languages
• injection and made for testing
• two-way data binding, change in the view is reflected in the model and viceversa
• directives, markers on the DOM element to attach specific behavior or transform the element (ng-repeat)
• powerful and scalable to support big projects
• really well documented, excellent support, big community, fast growing set of libraries
Disadvantages
• learning curve, takes time
• best practices needed to avoid bad programming patterns i.e. code can be written inside of templates
• complexity, some parts are a bit complex to understand
0D Bibliography
Dependency Injection
• Martin Fowler’s blog
• Inversion of Control
• SpringFramework
• Angular
• Google Guice
• JavaEE CDI

More Related Content

What's hot

Connection management
Connection managementConnection management
Connection management
Srilatha Kante
 
Refactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test AutomationRefactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test Automation
Stephen Fuqua
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
iOS testing
iOS testingiOS testing
iOS testing
Tomasz Janeczko
 
JEE Programming - 04 Java Servlets
JEE Programming - 04 Java ServletsJEE Programming - 04 Java Servlets
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
Test automation in Loris
Test automation in LorisTest automation in Loris
Test automation in Loris
Wasion Wang
 
Brief introduction into Padding Oracle attack vector
Brief introduction into Padding Oracle attack vectorBrief introduction into Padding Oracle attack vector
Brief introduction into Padding Oracle attack vector
Payampardaz
 
Write testable code in java, best practices
Write testable code in java, best practicesWrite testable code in java, best practices
Write testable code in java, best practices
Marian Wamsiedel
 
Glassfish JEE Server Administration - Module 4 Load Balancer
Glassfish JEE Server Administration - Module 4 Load BalancerGlassfish JEE Server Administration - Module 4 Load Balancer
Glassfish JEE Server Administration - Module 4 Load Balancer
Danairat Thanabodithammachari
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
Seb Rose
 
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra LohikaDependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Alex Tumanoff
 
my accadanic project ppt
my accadanic project pptmy accadanic project ppt
my accadanic project ppt
Manivel Thiruvengadam
 
Applying TDD to Legacy Code
Applying TDD to Legacy CodeApplying TDD to Legacy Code
Applying TDD to Legacy Code
Alexander Goida
 
JEE Programming - 07 EJB Programming
JEE Programming - 07 EJB ProgrammingJEE Programming - 07 EJB Programming
JEE Programming - 07 EJB Programming
Danairat Thanabodithammachari
 
Input validation slides of web application workshop
Input validation slides of web application workshopInput validation slides of web application workshop
Input validation slides of web application workshop
Payampardaz
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
Sam Brannen
 
2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded
2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded
2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded
Jonathan Gallimore
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
Marian Wamsiedel
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
kamal kotecha
 
Glassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionGlassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE Introduction
Danairat Thanabodithammachari
 

What's hot (20)

Connection management
Connection managementConnection management
Connection management
 
Refactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test AutomationRefactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test Automation
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
iOS testing
iOS testingiOS testing
iOS testing
 
JEE Programming - 04 Java Servlets
JEE Programming - 04 Java ServletsJEE Programming - 04 Java Servlets
JEE Programming - 04 Java Servlets
 
Test automation in Loris
Test automation in LorisTest automation in Loris
Test automation in Loris
 
Brief introduction into Padding Oracle attack vector
Brief introduction into Padding Oracle attack vectorBrief introduction into Padding Oracle attack vector
Brief introduction into Padding Oracle attack vector
 
Write testable code in java, best practices
Write testable code in java, best practicesWrite testable code in java, best practices
Write testable code in java, best practices
 
Glassfish JEE Server Administration - Module 4 Load Balancer
Glassfish JEE Server Administration - Module 4 Load BalancerGlassfish JEE Server Administration - Module 4 Load Balancer
Glassfish JEE Server Administration - Module 4 Load Balancer
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra LohikaDependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra Lohika
 
my accadanic project ppt
my accadanic project pptmy accadanic project ppt
my accadanic project ppt
 
Applying TDD to Legacy Code
Applying TDD to Legacy CodeApplying TDD to Legacy Code
Applying TDD to Legacy Code
 
JEE Programming - 07 EJB Programming
JEE Programming - 07 EJB ProgrammingJEE Programming - 07 EJB Programming
JEE Programming - 07 EJB Programming
 
Input validation slides of web application workshop
Input validation slides of web application workshopInput validation slides of web application workshop
Input validation slides of web application workshop
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
 
2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded
2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded
2015 JavaOne Java EE Connectors - The Secret Weapon Reloaded
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
 
Glassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionGlassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE Introduction
 

Similar to Dependency injection

Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
FalafelSoftware
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 
Patterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 appsPatterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 apps
Phil Leggetter
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
Stefan Oprea
 
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016
Steven Smith
 
Part 1 implementing a simple_web_service
Part 1 implementing a simple_web_servicePart 1 implementing a simple_web_service
Part 1 implementing a simple_web_servicekrishmdkk
 
Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injection
Neoito
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL Injection
Vortana Say
 
sfdsdfsdfsdf
sfdsdfsdfsdfsfdsdfsdfsdf
sfdsdfsdfsdf
guesta5433ea
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testabilitydrewz lin
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 
Fed London - January 2015
Fed London - January 2015Fed London - January 2015
Fed London - January 2015
Phil Leggetter
 
System verilog important
System verilog importantSystem verilog important
System verilog important
elumalai7
 
Java APIs - the missing manual
Java APIs - the missing manualJava APIs - the missing manual
Java APIs - the missing manual
Hendrik Ebbers
 
How to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that ScaleHow to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that Scale
Phil Leggetter
 

Similar to Dependency injection (20)

Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Patterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 appsPatterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 apps
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016
 
Part 1 implementing a simple_web_service
Part 1 implementing a simple_web_servicePart 1 implementing a simple_web_service
Part 1 implementing a simple_web_service
 
Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injection
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL Injection
 
Guice
GuiceGuice
Guice
 
Guice
GuiceGuice
Guice
 
sfdsdfsdfsdf
sfdsdfsdfsdfsfdsdfsdfsdf
sfdsdfsdfsdf
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability
 
Kasi Resume
Kasi ResumeKasi Resume
Kasi Resume
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Fed London - January 2015
Fed London - January 2015Fed London - January 2015
Fed London - January 2015
 
System verilog important
System verilog importantSystem verilog important
System verilog important
 
Java APIs - the missing manual
Java APIs - the missing manualJava APIs - the missing manual
Java APIs - the missing manual
 
How to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that ScaleHow to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that Scale
 

Recently uploaded

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 

Recently uploaded (20)

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 

Dependency injection

  • 1. Introduction to dependency injection. Spring, AngularJs Dependency Injection Brief introduction
  • 2. 0A pre-dependency injection Base example Advantages • easily enable testability • clean and concise code • testing client and service in isolation • client no need to know how to build the dependency object graph
  • 3. 0A pre-dependency injection Encapsulate creation 1 public class Emailer { 2 3 private SpellChecker spellChecker; 4 5 public Emailer() { 6 this.spellChecker = new SpellChecker(); 7 } 8 9 public void send(String text) { 10 //some implementation 11 } 12 } How to test? • Impossible: MockSpellChecker cannot substitute the internal spellChecker. Change implementation? • Impossible: Emailer encapsulates the creation of its dependencies.
  • 4. 0A pre-dependency injection Construction by hand 1 public class Emailer { 2 3 private SpellChecker spellChecker; 4 5 public void send(String text) { 6 //some implementation 7 } 8 9 public void setSpellChecker(SpellChecker spellChecker) { 10 this.spellChecker = spellChecker; 11 } 12 } Pros • Setter method: different flavors of the service can be injected. Cons • Object construction knowledge: on the client of the service. • Repeat wiring code • What about: encapsulation principle and altering the object graph?
  • 5. 0A pre-dependency injection Construction by hand test Pros • Setter method: MockSpellChecker can be easily injected. • Check if dependency has been correctly used. Cons • Repeat wiring code 1 @Test 2 public void testEmailerCheckSpelling() throws Exception { 3 4 MockSpellChecker mockSpellChecker = new MockSpellChecker(); 5 6 Emailer emailer = new Emailer(); 7 8 emailer.setSpellChecker(mockSpellChecker); 9 emailer.send("Hello World"); 10 11 assert mock.isSpellCheckingDone(); 12 }
  • 6. 0A pre-dependency injection Factory pattern Pros • Level of abstraction: Emailer client code is separated from the Emailer creation. • No internal knowledge: client code only use a specific factory to obtain a dependency. • Code clean and concise even with richer and more complex object graph. Cons • Extreme growth: factory classes tend to grow becoming really big and uncontrolled. 1 public class EmailerFactory { 2 3 public Emailer newFrenchEmailer() { 4 return new Emailer(new FrenchSpellChecker()); 5 } 6 7 public Emailer newJapaneseEmailer() { 8 return new Emailer(new JapaneseSpellChecker()); 9 } 10 } 11 12 //somewhere in the Client code 13 Emailer emailer = new EmailerFactory().newFrenchEmailer();
  • 7. 0A pre-dependency injection Factory pattern test Pros • Level of abstraction: Emailer client code is separated from the Emailer creation. • No internal knowledge: client code only use a specific factory to obtain a dependency. • Code clean and concise even with richer and more complex object graph. Cons • Extreme growth and maintenance: factory classes tend to grow becoming really big and uncontrolled. 1 public class EmailerFactory { 2 3 public Emailer newFrenchEmailer() { 4 return new Emailer(new FrenchSpellChecker()); 5 } 6 7 public Emailer newJapaneseEmailer() { 8 return new Emailer(new JapaneseSpellChecker()); 9 } 10 } 11 12 //somewhere in the Client code 13 Emailer emailer = new EmailerFactory().newFrenchEmailer();
  • 8. 0A pre-dependency injection Factory pattern test 1 public class EmailClient { 2 private Emailer emailer = new EmailerFactory.newEnglishEmailer(); 3 4 public void run() { 5 emailer.send("Hello World"); 6 } 7 } 8 9 @Test 10 public void testEmailClient() { 11 MockEmailer mock = new MockEmailer(); 12 EmailerFactory.set(mock); 13 14 new EmailClient.run(); 15 16 assert mock.correctlySent(); 17 } How & Cons to test with factory • Heavily modify factory class code to support testing for any client, state is shared thus code cleanup required after each test. • Create a factory mock or stub which can easily grow, it must accompany every variation of every service.
  • 9. 0B dependency injection Hollywood Principle: don’t call us; we’ll call you! Enable • Unawareness: client not aware of the Emailer kind. • Reversal of responsibilities: service not explicitly required. Pros • Easily enable testability via setter method or constructor injection (construction by hand). • Client no needs to know its dependencies and how to build them (factory pattern). • Dependencies are not required but provided (new). 1 public class Emailer { 2 public void send(String text) { 3 //some implementation 4 } 5 //some other methods 6 } 7 8 public class SimpleEmailClient { 9 private Emailer emailer; 10 11 public SimpleEmailClient(Emailer emailer) { 12 this.emailer = emailer; 13 } 14 public void sendEmail() { 15 emailer.send(readMessage()); 16 } 17 }
  • 10. 0B dependency injection Hollywood Principle: don’t call us; we’ll call you! embodies the Hollywood Principle Dependency Injector • Control over construction, wiring and assembly dependency or object graph. • Separates infrastructure code (wiring and construction) from application code (core purpose of the service)
  • 11. 0C dependency injection with Spring Spring Framework Spring • Open source application framework and inversion of control container for Java platform. • 1 October 2002, release 1.0.0, 7 July 2014, release 4.0.6, now 4.1.0 RC1 • active development and evolution to meet new programming requirements • Developed by Pivotal, a spin-out and joint venture of EMC Corporation and VMware. • Excellent documentation, clean and tested code, wide community and support. Projects • Spring Framework • core support for dependency injection, transaction management • Spring Data • provide consistent approach to data access • relational, non-relational, map-reduce and much more • Spring XD • simplify the development of big data applications • address analytics, batch jobs and data export • much more…
  • 12. 0C dependency injection with Spring Example: the client of the service Client • SimpleEmailClient decoupled from the EmailerService implementation. • SimpleEmailClient unaware of which implementation will be provided at runtime, programming against interfaces (OOP principle). 1 @Component 2 public class SimpleEmailClient { 3 4 final private EmailerService service; 5 6 @Autowired 7 public SimpleEmailClient(EmailerService service) { 8 this.service = service; 9 } 10 11 public void printMessage() { 12 System.out.println(this.service.getMessage()); 13 } 14 }
  • 13. 1 @Configuration 2 @ComponentScan 3 public class Application { 4 5 @Bean 6 MessageService mockEmailerService() { 7 return new EmailerService() { 8 public String getMessage() { 9 return "Hello World!"; 10 } 11 }; 12 } 13 14 public static void main(String[] args) { 15 ApplicationContext context = 16 new AnnotationConfigApplicationContext(Application.class); 17 SimpleEmailClient client = context.getBean(SimpleEmailClient.class); 18 client.printMessage(); 19 } 20 } 0C dependency injection with Spring Example: the service and the mock Injected mock implements the interface Injector wires client with its dependency
  • 14. 0C dependency injection with Spring Another example with integration test 1 @Entity 2 @Table(name = "MOVIE") 3 public class Movie extends AbstractEntity { 4 5 @Column(name = "TITLE") 6 private String title; 7 8 protected Movie() {...} 9 } Movie example • Simple Movie class annotated JPA 2.0, Hibernate, store and retrieve. • A service interface is given. • Repository and service should be created and implemented to support basic CRUD operations. Code on Github • The code of the example is available on Github 1 public interface MovieService { 2 3 List<Movie> findAll(); 4 5 Movie create(final Movie movie); 6 7 Movie delete(final Movie movie); 8 9 Movie update(final Movie movie); 10 }
  • 15. 0C dependency injection with Spring Integration test: another example Movie repository and service • No need to implement basic CRUD methods as findAll(), delete(), save() etc. • Methods are resolved based on attribute names, just declare in the repository interface findById() to have it automatically implemented at runtime where Id is the attribute. • More complex methods have to be directly implemented. • Clean and concise code, no need to implement explicit SQL query, reduce boiler-plate code. 1 @Repository 2 @Transactional 3 public class MovieServiceImpl implements MovieService { 4 5 @Autowired 6 private MovieRepository movieRepository; 7 8 @Override 9 public final List<Movie> findAll() { 10 return Lists.newArrayList(movieRepository.findAll()); 11 } 12 ... 13 } 1 public interface MovieRepository extends CrudRepository<Movie, Long> { 2 }
  • 16. 0C dependency injection with Spring Integration test: another example 1 @ActiveProfiles("test") 2 public class MovieServiceImplTest extends AbstractServiceImplTest { 3 4 5 @Autowired 6 private MovieService movieService; 7 8 @DataSets(setUpDataSet = "/MovieServiceImplTest.xls") 9 @Test 10 public final void testFindAll() throws Exception { 11 List<Movie> results = movieService.findAll(); 12 13 assertNotNull(results); 14 assertThat(results.size(), equalTo(3)); 15 } 16 } Injected implementation based on the interface Build ApplicationContext based on the active profile Insert into the data source tables from xls file before the method execution. After method execution automatic rollback.
  • 17. 1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(classes = {ServiceTestConfig.class}) 3 @TestExecutionListeners({ServiceTestExecutionListener.class}) 4 @ActiveProfiles("test") 5 public abstract class AbstractServiceImplTest extends AbstractTransactionalJUnit4SpringContextTests { 6 7 @PersistenceContext 8 protected EntityManager em; 9 } 0C dependency injection with Spring ApplicationContext configuration with a specific data source, schema and data. Based on a standard JUnit Runner. Standard javax.persistence, hibernate jpa 2.0 api. Listener to intercept standard test execution lifecycle. Test • Non invasive solution to build integration tests based on standard frameworks like JUnit and Hibernate • It works on top of standard frameworks providing clean and concise code for complex test cases. Integration test: another example
  • 18. 1 @Configuration 2 @ImportResource("classpath:datasource-tx-jpa.xml") 3 @ComponentScan(basePackages = {"com.contrastofbeauty.movieapp.service"}) 4 @Profile("test") 5 public class ServiceTestConfig { 6 7 @Bean 8 public DataSource dataSource() { 9 return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .addScript("classpath:schema.sql").build(); 10 } 11 12 @Bean(name = "databaseTester") 13 public DataSourceDatabaseTester dataSourceDatabaseTester() { 14 DataSourceDatabaseTester databaseTester = new DataSourceDatabaseTester(dataSource()); 15 return databaseTester; 16 } 17 18 @Bean(name = "xlsDataFileLoader") 19 public XlsDataFileLoader xlsDataFileLoader() { 20 return new XlsDataFileLoader(); 21 } 22 } 0C dependency injection with Spring Reuse transaction and JPA configuration DbUnit classes to load xls files and setup the data source Data source configuration Integration test: another example
  • 19. 0C dependency injection with Spring Considerations Spring Debate • Spring is not standard. • Just pointless, Spring promote the use of standard. • Really important to have a standard, but if the solution of your problem is not standard you will wait three or more years? • Spring Social (JSR-357 rejected in 2012, really opened debate on future) • Widespread container testing done with Arquillan, JBoss specific. • Spring is not compatible. • Free to use JPA, CDI annotations etc. • Use all Java EE technologies and additional features provide by Spring. • Spring works with all Java EE application servers and more like Jetty and Tomcat. • Spring is a must, best choice • Not always and Just pointless, what are your requirements? • Spring is a meta-framework, highly scalable, highly configurable but complex. • Spring configuration could be difficult to manage when the project grows. • Use only Spring dependency injection? May be consider what already provided by the container.
  • 20. 0D AngularJS $scope, $controller, $http and test with mocks Where to find the code • Github for source code: angulajs-popcorn • Plunker online live editing: application and testing
  • 21. 0D AngularJS Definition MVC Design Pattern • View is projection of the model through the HTML template. • Controller is a Javascript constructor function used to augment the Angular scope. • Scope an object referring to application model, used along with data model, template and controller to keep models and views in sync. Template Model View (model/view data binding) Root Scope movieController Scope movies: Array <!DOCTYPE html> <html lang="en" ng-app="movieApp"> <body ng-controller="movieController"> <ul> <li ng-repeat="movie in movies"> {{movie.title}} </li> </ul> </body> </html> Repetear Scope movie: Object ▪ Commando ▪ Raw Deal ▪ Predator n-times ng-controller ng-app n-times
  • 22. 0D AngularJS dependency injection Definition Angular Injection • $scope injected into controller, scope is the glue between application controller and the view. • $http injected, an Angular service to facilitate the communication with the remote HTTP server. • $httpBackend mock of the real $httpBackend, backend used by the service to delegate to XMLHttpRequest. 1 var movieApp = angular.module('movieApp', ['xeditable', 'ngMockE2E']); 2 3 movieApp.controller('MovieController', function($scope, $http) { 4 5 $scope.loadMovies = function() { 6 return $http.get("/movies").success(function(response){ 7 $scope.movies = response; 8 }); 9 }; 10 }); 11 12 // --------------- mock $http requests ---------------------- 13 movieApp.run(function($httpBackend) { 14 $httpBackend.whenGET('/movies').respond([ 15 {id: 1, title: "Commando", actors: "Arnold Schwarzenegger"}, 16 {id: 2, title: "Raw Deal", actors: "Arnold Schwarzenegger"}, 17 {id: 3, title: "Predator", actors: "Arnold Schwarzenegger"} 18 ]) 19 });
  • 23. 0D AngularJS dependency injection Test controller using Jasmine BDD framework 1 describe('Movie Controller test suite', function() { 2 3 describe('MovieController', function() { 4 5 var $scope, $httpBackend, createController, $http; 6 7 beforeEach(module('movieApp')); 8 9 beforeEach(inject(function($controller, $rootScope, _$httpBackend_, _$http_) { 10 $scope = $rootScope.$new(); 11 $httpBackend = _$httpBackend_; 12 $http = _$http_; 13 14 createController = function() { 15 return $controller('MovieController', { 16 $scope: $scope 17 }); 18 }; 19 })); 20 21 it("should GET all the movies", function() { 22 $httpBackend.expectGET('/movies').respond(200, [{}, {}, {}]); 23 createController(); 24 $scope.loadMovies(); 25 $httpBackend.flush(); 26 expect($scope.movies.length).toBe(3); 27 }); 28 }); Jasmine description suite Setup test context injecting Angular services Jasmine test declaration or spec Obtain the controller and invoke the function to test Assert the result
  • 24. 0D AngularJS Considerations Advantages • clean separation of concerns • template is pure HTML, no other languages • injection and made for testing • two-way data binding, change in the view is reflected in the model and viceversa • directives, markers on the DOM element to attach specific behavior or transform the element (ng-repeat) • powerful and scalable to support big projects • really well documented, excellent support, big community, fast growing set of libraries Disadvantages • learning curve, takes time • best practices needed to avoid bad programming patterns i.e. code can be written inside of templates • complexity, some parts are a bit complex to understand
  • 25. 0D Bibliography Dependency Injection • Martin Fowler’s blog • Inversion of Control • SpringFramework • Angular • Google Guice • JavaEE CDI