Spring integration testing with Spock
I'm currently working on a project where we use Spring Framework 4.0 and Spock for testing. Unit testing in Spock is really great, but there are some lacks in integration testing - especially in mocking Spring beans. Our requirement is to start up Spring context but with particular bean replaced with mock.
The easiest part we had do implement is just regular Spring test specification:
The problem here is that getAuthToken method have to find this client configuration (including for example some keys data). It's done by invoking configurationService.getClientConfig(CLIENT_ID) method. So the best option here is to mock this invocation and return some predefined fixture.
What comes on my mind in that moment is Springockito project. Due to JavaConfig is our application springockito-annotations module have to be used. After adding proper dependency you just need to add "loader = SpringockitoAnnotatedContextLoader" into @ContextConfiguration.
And last but not least part - setting up mock. After many tries it was impossible for me to drive Spock mocking framework to do that, so I come back to old-school Mockito. Finally my test class looks as follows:
And that leads our test to working properly :)
The easiest part we had do implement is just regular Spring test specification:
@ContextConfiguration( classes = [GatewayConfig, PaymentServiceConfig]) class PaymentServiceSpecIT extends Specification { final String CLIENT_ID = "54321" @Inject PaymentService paymentService def "Should return auth token"() { when: def token = paymentService.getAuthToken(CLIENT_ID) then: token } }
The problem here is that getAuthToken method have to find this client configuration (including for example some keys data). It's done by invoking configurationService.getClientConfig(CLIENT_ID) method. So the best option here is to mock this invocation and return some predefined fixture.
What comes on my mind in that moment is Springockito project. Due to JavaConfig is our application springockito-annotations module have to be used. After adding proper dependency you just need to add "loader = SpringockitoAnnotatedContextLoader" into @ContextConfiguration.
And last but not least part - setting up mock. After many tries it was impossible for me to drive Spock mocking framework to do that, so I come back to old-school Mockito. Finally my test class looks as follows:
@ContextConfiguration( classes = [GatewayConfig, PaymentServiceConfig], loader = SpringockitoAnnotatedContextLoader) class PaymentServiceSpecIT extends Specification { final String CLIENT_ID = "54321" @Inject PaymentService paymentService @Inject @ReplaceWithMock ConfigurationService configService def "Should return auth token"() { given: given(configService.getClientConfig(CLIENT_ID)) .willReturn(DataFixtures.clientConfig()) when: def token = paymentService.getAuthToken(CLIENT_ID) then: token } }
And that leads our test to working properly :)
Comments
It's worth noting that there is a pitfall when integrating Spock and Spring Boot with implicit property sources (application.properties etc.). In order to get them loaded in the first example (without Springockito), we should additionally specify "loader = SpringApplicationContextLoader" in @ContextConfiguration. In the second example, with Springockito, we cannot do that since we need a SpringockitoAnnotatedContextLoader, but adding "initializers = ConfigFileApplicationContextInitializer" to @ContextConfiguration solves the problem.
Alternatively, we could define our property sources explicitly in our JavaConfig e.g. using @PropertySource("application.properties") and leave @ContextConfigurations as presented.
Do you have a better suggestion on how to overcome this pitfall?
@Bean
ConfigurationService mockedConfiguratinService(){
ConfigurationService cs = Mock()
cs.getClientConfig(CLIENT_ID)>>DataFixtures.clientConfig()
cs
}
in a configuration class?
The Article on Spring integration testing with Spock is amazing give detail information about it .Thanks for Sharing the information about Integration Testing.
Software Testing Company
software testing training in chennai
Software testing course in chennai
Mobile Testing Training in Chennai
Mobile Appium Training in Chennai
Ionic Training in Chennai
Placement Training in Chennai
Excel Training in Chennai
VMware Training in Chennai
Microsoft Dynamics CRM Training in Chennai
French Classes in Chennai
Big Data Training in Chennai
Hadoop Training in Chennai
Android Training in Chennai
Selenium Training in Chennai
Digital Marketing Course in Chennai
JAVA Training in Chennai
web designing course in velachery
web designing course in tambaram
Aviation Courses in Chennai
air hostess institute in Chennai
airline and airport management courses in Chennai
airport ground staff training courses in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai
Thank you for publishing this blog, really awesome. An only experienced professional can do this.
Spring Training in Chennai
Spring framework Training in Chennai
spring Training in Anna Nagar
Hibernate Training in Chennai
Hibernate course in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
Spring Training in Chennai
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.
And also those who are looking for
Web Designing Training Institute in Chennai
SEO Training Institute in Chennai
Photoshop Training Institute in Chennai
PHP & Mysql Training Institute in Chennai
Android Training Institute in Chennai
Microsoft Dynamics crm Training in Chennai
Microsoft Dynamics Training in Chennai
Microsoft Dynamics crm Training institutes in Chennai
Salesforce Training in Chennai
AngularJS Training in Chennai
ccna course in Chennai
Microsoft Dynamics crm Training in Velachery
Microsoft Dynamics crm Training in T Nagar
Microsoft Dynamics crm Training in OMR
web designing course in chennai with placement
php mysql course in chennai
magento training in chennai
Kindly visit us @
Luxury Boxes
Premium Packaging
Luxury Candles Box
Earphone Packaging Box
Wireless Headphone Box
Innovative Packaging Boxes
Wedding gift box
Leather Bag Packaging Box
Cosmetics Packaging Box
Luxury Chocolate Boxes
Kindly visit us @
100% Job Placement
Best Colleges for Computer Engineering
Biomedical Engineering Colleges in Coimbatore
Best Biotechnology Colleges in Tamilnadu
Biotechnology Colleges in Coimbatore
Biotechnology Courses in Coimbatore
Best MCA Colleges in Tamilnadu
Best MBA Colleges in Coimbatore
Engineering Courses in Tamilnadu
Engg Colleges in Coimbatore
ui ux design course in Chennai
ux design course in chennai
ui ux course in chennai
Web Designing Course in chennai
web designing training in chennai
AngularJS Training in Chennai
ccna course in Chennai
ui ux design course in Velachery
ui ux design course in T Nagar
ui ux design course in OMR
Oracle DBA Training in Chennai
oracle dba training
Advanced Excel Training in Chennai
Spark Training in Chennai
Tableau Training in Chennai
Oracle Training in Chennai
Pega Training in Chennai
Embedded System Course Chennai
Unix Training in Chennai
Oracle DBA Training in T Nagar
Oracle DBA Training in OMR
ReactJS Training in Chennai
ReactJS course in Chennai
AngularJS Training Institute in Chennai
Angular 6 Training in Chennai
Web Development courses in Chennai
PHP Training Institute in Chennai
ccna course in Chennai
ReactJS Training in Porur
ReactJS Training in Tambaram
ReactJS Training in Adyar
web designing and development course training institute in Chennai with placement
PHP MySQL programming developer course training institute in chennai with placement
Magento 2 Developer course training institute in chennai
web designing classes in chennai | web designing training institute in chennai
web designing and development course in chennai | web designing courses in Chennai
best institute for web designing in chennai | web designing course with placement in chennai
web designer courses in chennai | best institute for web designing Classes in Chennai
web designing courses in chennai | web designing institute in chennai | web designing training institute in chennai
web designing training in chennai | web design and development institute
web designing classes in Chennai | web designer course in Chennai
web designingtraining course in chennai with placement | web designing and development Training course in chennai
Web Designing Institute in Chennai | Web Designing Training in Chennai
website design course | Web designing course in Chennai
best software testing training in chennai
best software testing training institute in chennai with placement
software testing training
courses
software testing training and placement
software testing training online
software testing class
software testing classes in chennai
best software testing courses in chennai
automation testing courses in chennai
Thanks for sharing.
Ionic Training in Chennai
Ionic Corporate Training
german classes
Best IELTS Coaching in Chennai
learn Japanese in Chennai
TOEFL Coaching Centres in Chennai
content writing course in chennai
spanish coaching in chennai
Ionic Training in Porur
Ionic Training in OMR
Final Year Project Domains for CSE
Final Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
Pouch Manufacturers
fertilizer bag manufacturers
Lyrics with music
ppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company
Washing Machine Repair In Faridabad
LG Washing Machine Repair In Faridabad
Bosch Washing Machine Repair In Faridabad
Whirlpool Washing Machine Repair In Faridabad
Samsung Washing Machine Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida
iso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification cost
iso consultants in noida
iso 27001 certification services
ISO 9001 Certification in Noida
iso 22000 certification in Delhi
Very good Keep it up.
IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
A very interesting blog....
Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..
Vermicompost Manufacturers | Best Vermicompost in chennai
Magento Development Training Course in Chennai Zuan Education
Selenium Training Course in Chennai Zuan Education
Tours and Travels in Madurai | Best tour operators in Madurai
Best travel agency in Madurai | Best Travels in Madurai
Hi, Very nice article. I hope you will publish again such type of post. Thank you!
Corporate gifts ideas | Corporate gifts
Corporate gifts singapore | Corporate gifts in singapore
Promotional gifts singapore | Corporate gifts wholesale Singapore
Thanks for sharing such a great blog
Vermicompost manufacturers in Tamilnadu | Vermicompost in Tamilnadu
Vermicompost Manufacturers | Vermicompost Suppliers
Vermicompost in Coimbatore | Vermicompost manufacturers in Chennai
Vermicompost in chennai | Best Vermicompost in chennai
.
Corporate gifts ideas | Corporate gifts
Corporate gifts singapore | Corporate gifts in singapore
Promotional gifts singapore | Corporate gifts wholesale Singapore
Business card holder singapore | T shirts supplier singapore
Thumb drive supplier singapore | Leather corporate gifts singapore
Forex Signals Forex Strategies Forex Indicators Forex News Forex World
Corporate gifts ideas | Corporate gifts
Corporate gifts singapore | Corporate gifts in singapore
Promotional gifts singapore | corporate gifts supplier
DigiPeek is the best SEO & Link Building Service Provider In The World. I have 7+ Years Experience To Build SEO, Backlinks & Improve Website Ranking.
If you need Profile Backlinks, Forum Backlinks, Dofollow Backlinks, Manual Backlinks, Trusted SEO Backlinks, Increase Domain Rating Then You Will Contact Me.
I am glad to help You!
Let's TRY!
Security Guard License | Security License | Ontario Security license | Security License Ontario | Ontario Security Guard License | Security Guard License Ontario
Hi, Very nice article. I hope you will publish again such type of post. Thank you!
Corporate gifts ideas | Corporate gifts
Corporate gifts singapore | Corporate gifts in singapore
Promotional gifts singapore | corporate gifts supplier
Iphone service center in tnagar | Iphone service center in chennai
Lenovo mobile service center in Tnagar | Lenovo Mobile service center in chennai
Moto service center in t nagar | Motorola service center in t nagar
Moto Service Center in Chennai | Motorola Service Center in chennai
B2B Marketplace
Wholesalers
Our Service Partners
Refrigerator Repair Service
Satta King
Mumbai Call Girls
second hand mobile phones for sale
Woodland showroom in madurai | Skechers showroom in Madurai
Puma showroom in madurai
But what makes Indonesia so great? Major cities like Jakarta, Bandung, Yogyakarta, and Surabaya, an impressive university system, and low taxes means the Sunshine country is ripe with opportunity for savvy business owners. Keep reading to learn more about why Indonesia could be the perfect spot to start your new business!
Kami menawarkan layanan Jasa SEO terbaik di Bali dengan Strategi Optimasi Situs Website secara tepat dan akurat sesuai dengan prinsip kerja mesin pencari.
Membantu memfasilitasi pencocokan dan komunikasi peluang kerja antara pencari kerja dan pemberi kerja, di Bali, Denpasar, Legian, Seminyak, Canggu, Sanur, Nusa Dua, Tuban, Jimbaran.
It’s a nice blog with very useful information!!!
swift developer training in chennai
Cyber security course fees in chennai
Testing Courses in Chennai
Artificial intelligence training in chennai
Full stack developer training in chennai
Best Graphic Design courses in
Chennai
vé máy bay đi Mỹ hạng thương gia
giá vé máy bay vinh sài gòn hôm nay
vé máy bay từ cần thơ đi hà nội
giá vé máy bay đi đà lạt khứ hồi
các chuyến bay từ mỹ về việt nam hiện nay
taxi sân bay nội bài
combo du lịch quy nhơn
UI UX Design Studio
AWS Training in Chennai
DevOps Training in Chennai
We at Antino Labs believe in redefining and refining our model to suit the industry's requirements. Antino Labs' several years of experience in the market has let us register our global presence. Antino Labs' has the vision to become the world's most trusted partner for digital transformation and we aim to become a brand that defines innovation and the latest technology. We offer clients a one-stop solution for all their interests regarding IT consulting services in Gurgaon and UI/UX design services in Gurgaon.
Very Informative and useful... Keep it up the great work. I really appreciate your post.
https://salsabeelahmedandco.com/company-registration-in-bangalore/
https://salsabeelahmedandco.com/ca-firms-in-bangalore/
https://salsabeelahmedandco.com/accounting-services-in-bangalore/
https://salsabeelahmedandco.com/gst-consultants-in-bangalore/
https://salsabeelahmedandco.com/public-limited-company/
https://salsabeelahmedandco.com/private-limited-company-in-india/
https://salsabeelahmedandco.com/one-person-company-in-india/
https://salsabeelahmedandco.com/partnership-firm-in-bangalore/
https://salsabeelahmedandco.com/limited-liability-partnership-llp/
Packers and Movers in Chennai | Packers and Movers in Trichy
Packers and Movers in Theni | Packers and Movers in Karur
Packers and Movers in Madurai
Web Development Services in Gurgaon
Best Spoken English Classes in Chennai | Spoken English Training Institute in Chennai
Low Cost Murukku Machine in Tamilnadu
Automatic Murukku Machine in Tamilnadu
Kai Murukku Machine in Tamilnadu
Chakli Making Machine in Tamilnadu
IT Support Services London
Introducing a new application named Engadoctor. Especially design with newly emerging technology for Online Doctor Consultation & Book Online Doctor Appointment.
Adarsh Park Heights
Adarsh Park Heights price
Adarsh Property
Sumadharu Folium
SUMADHURA FOLIUM