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
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
web designing course in chennai with placement
php mysql course in chennai
magento training in chennai
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
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
Forex Signals Forex Strategies Forex Indicators Forex News Forex World
Security Guard License | Security License | Ontario Security license | Security License Ontario | Ontario Security Guard License | Security Guard License Ontario
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!
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
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.
Web Development Services in Gurgaon
Best Spoken English Classes in Chennai | Spoken English Training Institute in Chennai
Introducing a new application named Engadoctor. Especially design with newly emerging technology for Online Doctor Consultation & Book Online Doctor Appointment.