testing with spring
DESCRIPTION
TRANSCRIPT
![Page 1: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/1.jpg)
Dependency Injection and Testing with the Spring framework
Leonardo Borges www.leonardoborges.com
![Page 2: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/2.jpg)
Dependency Injection and Testing with the Spring framework
Why dependency injection? It frees your code of knowing about the underlying
implementation Eliminates the necessity of lookup codes, thus
saving development time Eases the testing environment
![Page 3: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/3.jpg)
Dependency Injection and Testing with the Spring framework
A first example
![Page 4: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/4.jpg)
Dependency Injection and Testing with the Spring framework
Testing the Service class
![Page 5: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/5.jpg)
Dependency Injection and Testing with the Spring framework
Great! A JDBC DAO! But I was planning to use Hibernate! Problem
The service object knows the DAO's implementation class
Solution Make the DAO classes implement an interface
![Page 6: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/6.jpg)
Dependency Injection and Testing with the Spring framework
The new DAO classes
![Page 7: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/7.jpg)
Dependency Injection and Testing with the Spring framework
But to achieve independency, we need to change the service class
![Page 8: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/8.jpg)
Dependency Injection and Testing with the Spring framework
Dependency Injection kicks in
![Page 9: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/9.jpg)
Dependency Injection and Testing with the Spring framework
It's all about context
![Page 10: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/10.jpg)
Dependency Injection and Testing with the Spring framework
Testing the new Service class
![Page 11: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/11.jpg)
Dependency Injection and Testing with the Spring framework
It's all about [test] context
![Page 12: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/12.jpg)
Dependency Injection and Testing with the Spring framework
Results from testing the new Service class
![Page 13: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/13.jpg)
Dependency Injection and Testing with the Spring framework
Conclusions With dependency injection and spring, you can
achieve a great level of independence while keeping your code highly testable trough the easy substitution of your real objects by mocks.
![Page 14: Testing with Spring](https://reader033.vdocuments.us/reader033/viewer/2022042613/54b685844a7959d4728b464a/html5/thumbnails/14.jpg)
Dependency Injection and Testing with the Spring framework
Further Resources www.springframework.org www.leonardoborges.com