SlideShare a Scribd company logo
Spock-Framework: The Logical Choice
for Modern Testing
Introduction
We'll look at Spock, a Groovy testing framework. Mainly, Spock aims to be a more powerful
alternative to the traditional JUnit stack, by leveraging Groovy features.
By making use of Groovy, Spock introduces new and expressive ways of testing our Java
applications, which simply aren't possible in ordinary Java code. We'll explore some of Spock's
high-level concepts during this article, with some practical step-by-step examples.
Prerequisite
You have some knowledge about spring-boot and testing concept like mocking, Stubbing, etc.
Here we learn the Spock framework with the Spring boot application.
To use Spock in spring boot, add the below dependency into pom.xml
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.3-groovy-4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.4</version>
</dependency>
And add a plugin for groovy support
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
Simple test using Spock framework
Make groovy class and extend the specification to it (spock.lang.specification).
For testing we made one simple method.
@SpringBootTest
class Test1 extends Specification {
def "Simple Test"(){
expect:
1+1 == 2
}
}
Here “def” keyword is used to define method after this you can add method discerption.
Now run the test
Output:
You see Test Case Passed successfully.
Controller Test-Cases
First, create a ‘Hello’ controller
@RestController
public class Hello {
@GetMapping("/hello")
public String greeting (){
return "Hello World! ";
}
Now for this controller, we are writing test cases using Spock.
class HelloTest extends Specification {
@MockBean
def cont = new Hello ()
MockMvc mvc = MockMvcBuilders.standaloneSetup(cont). build()
def "when get is performed then the response has status 200 and content is 'Hello
world!'"() {
expect: "Status is 200 and the response is 'Hello world!'"
mvc.perform(MockMvcRequestBuilders.get("/hello"))
.andExpect(status().isOk())
.andReturn()
.response
.contentAsString == "Hello World! "
}
}
Output:
Keywords:
1. Expect: This is a way of performing our stimulus and assertion within the same block.
Depending on what we find more expressive, we may or may not choose to use this
block
2. When – This is where we provide a stimulus to what is under test. In other words, where
we invoke our method under test
3. Then – This is where the assertions belong. In Spock, these are evaluated as plain
Boolean assertions, which will be covered later
Important Tips
1) All the blocks in a Spock based spec are optional. However, if present “when” and “then”
should appear together in sequence.
2) If you just need to validate an assertion (and you don’t have a need of when block), you can
use “expect” block. It can be used to assert pre-conditions even before when and then blocks
(and can be used multiple times in a test).
Originally published by: Spock Framework

More Related Content

Similar to Spock-Framework: The Logical Choice for Modern Testing

JavaScript development methodology
JavaScript development methodologyJavaScript development methodology
JavaScript development methodology
Aleksander Fabijan
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)
jaxLondonConference
 
Java 9 and the impact on Maven Projects (JavaOne 2016)
Java 9 and the impact on Maven Projects (JavaOne 2016)Java 9 and the impact on Maven Projects (JavaOne 2016)
Java 9 and the impact on Maven Projects (JavaOne 2016)
Robert Scholte
 
Agile Swift
Agile SwiftAgile Swift
Agile Swift
Godfrey Nolan
 
Spock
SpockSpock
Java 7 Modularity: a View from the Gallery
Java 7 Modularity: a View from the GalleryJava 7 Modularity: a View from the Gallery
Java 7 Modularity: a View from the Gallerynjbartlett
 
Arquillian in a nutshell
Arquillian in a nutshellArquillian in a nutshell
Arquillian in a nutshell
Brockhaus Group
 
RoboSpock
RoboSpockRoboSpock
Arquillian in a nutshell
Arquillian in a nutshellArquillian in a nutshell
Arquillian in a nutshell
Brockhaus Consulting GmbH
 
RoboSpock Poznań ADG 2016
RoboSpock Poznań ADG 2016RoboSpock Poznań ADG 2016
RoboSpock Poznań ADG 2016
Przemek Jakubczyk
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Mani Sarkar
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 world
Yura Nosenko
 
Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)
Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)
Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)
Robert Scholte
 
Bt0074, oops with java
Bt0074, oops with javaBt0074, oops with java
Bt0074, oops with java
smumbahelp
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
ranjanadeore1
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
loidasacueza
 
4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...
4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...
4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...
PROIDEA
 

Similar to Spock-Framework: The Logical Choice for Modern Testing (20)

JavaScript development methodology
JavaScript development methodologyJavaScript development methodology
JavaScript development methodology
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)
 
Java 9 and the impact on Maven Projects (JavaOne 2016)
Java 9 and the impact on Maven Projects (JavaOne 2016)Java 9 and the impact on Maven Projects (JavaOne 2016)
Java 9 and the impact on Maven Projects (JavaOne 2016)
 
Agile Swift
Agile SwiftAgile Swift
Agile Swift
 
Spock
SpockSpock
Spock
 
Java 7 Modularity: a View from the Gallery
Java 7 Modularity: a View from the GalleryJava 7 Modularity: a View from the Gallery
Java 7 Modularity: a View from the Gallery
 
Arquillian in a nutshell
Arquillian in a nutshellArquillian in a nutshell
Arquillian in a nutshell
 
RoboSpock
RoboSpockRoboSpock
RoboSpock
 
Arquillian in a nutshell
Arquillian in a nutshellArquillian in a nutshell
Arquillian in a nutshell
 
RoboSpock Poznań ADG 2016
RoboSpock Poznań ADG 2016RoboSpock Poznań ADG 2016
RoboSpock Poznań ADG 2016
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
 
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 world
 
Spock pres
Spock presSpock pres
Spock pres
 
Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)
Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)
Java 9 and the impact on Maven Projects (ApacheCon Europe 2016)
 
Bt0074, oops with java
Bt0074, oops with javaBt0074, oops with java
Bt0074, oops with java
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
 
4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...
4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...
4Developers 2015: Sprytniejsze testowanie kodu Java ze Spock Framework - Marc...
 
Spring
SpringSpring
Spring
 

More from Inexture Solutions

Spring Boot for WebRTC Signaling Servers: A Comprehensive Guide
Spring Boot for WebRTC Signaling Servers: A Comprehensive GuideSpring Boot for WebRTC Signaling Servers: A Comprehensive Guide
Spring Boot for WebRTC Signaling Servers: A Comprehensive Guide
Inexture Solutions
 
Mobile App Development Cost 2024 Budgeting Your Dream App
Mobile App Development Cost 2024 Budgeting Your Dream AppMobile App Development Cost 2024 Budgeting Your Dream App
Mobile App Development Cost 2024 Budgeting Your Dream App
Inexture Solutions
 
Data Serialization in Python JSON vs. Pickle
Data Serialization in Python JSON vs. PickleData Serialization in Python JSON vs. Pickle
Data Serialization in Python JSON vs. Pickle
Inexture Solutions
 
Best EV Charging App 2024 A Tutorial on Building Your Own
Best EV Charging App 2024 A Tutorial on Building Your OwnBest EV Charging App 2024 A Tutorial on Building Your Own
Best EV Charging App 2024 A Tutorial on Building Your Own
Inexture Solutions
 
What is a WebSocket? Real-Time Communication in Applications
What is a WebSocket? Real-Time Communication in ApplicationsWhat is a WebSocket? Real-Time Communication in Applications
What is a WebSocket? Real-Time Communication in Applications
Inexture Solutions
 
SaaS Application Development Explained in 10 mins
SaaS Application Development Explained in 10 minsSaaS Application Development Explained in 10 mins
SaaS Application Development Explained in 10 mins
Inexture Solutions
 
Best 7 SharePoint Migration Tools of 2024
Best 7 SharePoint Migration Tools of 2024Best 7 SharePoint Migration Tools of 2024
Best 7 SharePoint Migration Tools of 2024
Inexture Solutions
 
Spring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdfSpring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdf
Inexture Solutions
 
Best Features of Adobe Experience Manager (AEM).pdf
Best Features of Adobe Experience Manager (AEM).pdfBest Features of Adobe Experience Manager (AEM).pdf
Best Features of Adobe Experience Manager (AEM).pdf
Inexture Solutions
 
React Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for DevelopersReact Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for Developers
Inexture Solutions
 
Python Kafka Integration: Developers Guide
Python Kafka Integration: Developers GuidePython Kafka Integration: Developers Guide
Python Kafka Integration: Developers Guide
Inexture Solutions
 
What is SaMD Model, Benefits, and Development Process.pdf
What is SaMD Model, Benefits, and Development Process.pdfWhat is SaMD Model, Benefits, and Development Process.pdf
What is SaMD Model, Benefits, and Development Process.pdf
Inexture Solutions
 
Unlocking the Potential of AI in Spring.pdf
Unlocking the Potential of AI in Spring.pdfUnlocking the Potential of AI in Spring.pdf
Unlocking the Potential of AI in Spring.pdf
Inexture Solutions
 
Mobile Banking App Development Cost in 2024.pdf
Mobile Banking App Development Cost in 2024.pdfMobile Banking App Development Cost in 2024.pdf
Mobile Banking App Development Cost in 2024.pdf
Inexture Solutions
 
Education App Development : Cost, Features and Example
Education App Development : Cost, Features and ExampleEducation App Development : Cost, Features and Example
Education App Development : Cost, Features and Example
Inexture Solutions
 
Firebase Push Notification in JavaScript Apps
Firebase Push Notification in JavaScript AppsFirebase Push Notification in JavaScript Apps
Firebase Push Notification in JavaScript Apps
Inexture Solutions
 
Micronaut Framework Guide Framework Basics and Fundamentals.pdf
Micronaut Framework Guide Framework Basics and Fundamentals.pdfMicronaut Framework Guide Framework Basics and Fundamentals.pdf
Micronaut Framework Guide Framework Basics and Fundamentals.pdf
Inexture Solutions
 
Steps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MACSteps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MAC
Inexture Solutions
 
Python Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txtPython Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txt
Inexture Solutions
 
Gain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring BatchGain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring Batch
Inexture Solutions
 

More from Inexture Solutions (20)

Spring Boot for WebRTC Signaling Servers: A Comprehensive Guide
Spring Boot for WebRTC Signaling Servers: A Comprehensive GuideSpring Boot for WebRTC Signaling Servers: A Comprehensive Guide
Spring Boot for WebRTC Signaling Servers: A Comprehensive Guide
 
Mobile App Development Cost 2024 Budgeting Your Dream App
Mobile App Development Cost 2024 Budgeting Your Dream AppMobile App Development Cost 2024 Budgeting Your Dream App
Mobile App Development Cost 2024 Budgeting Your Dream App
 
Data Serialization in Python JSON vs. Pickle
Data Serialization in Python JSON vs. PickleData Serialization in Python JSON vs. Pickle
Data Serialization in Python JSON vs. Pickle
 
Best EV Charging App 2024 A Tutorial on Building Your Own
Best EV Charging App 2024 A Tutorial on Building Your OwnBest EV Charging App 2024 A Tutorial on Building Your Own
Best EV Charging App 2024 A Tutorial on Building Your Own
 
What is a WebSocket? Real-Time Communication in Applications
What is a WebSocket? Real-Time Communication in ApplicationsWhat is a WebSocket? Real-Time Communication in Applications
What is a WebSocket? Real-Time Communication in Applications
 
SaaS Application Development Explained in 10 mins
SaaS Application Development Explained in 10 minsSaaS Application Development Explained in 10 mins
SaaS Application Development Explained in 10 mins
 
Best 7 SharePoint Migration Tools of 2024
Best 7 SharePoint Migration Tools of 2024Best 7 SharePoint Migration Tools of 2024
Best 7 SharePoint Migration Tools of 2024
 
Spring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdfSpring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdf
 
Best Features of Adobe Experience Manager (AEM).pdf
Best Features of Adobe Experience Manager (AEM).pdfBest Features of Adobe Experience Manager (AEM).pdf
Best Features of Adobe Experience Manager (AEM).pdf
 
React Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for DevelopersReact Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for Developers
 
Python Kafka Integration: Developers Guide
Python Kafka Integration: Developers GuidePython Kafka Integration: Developers Guide
Python Kafka Integration: Developers Guide
 
What is SaMD Model, Benefits, and Development Process.pdf
What is SaMD Model, Benefits, and Development Process.pdfWhat is SaMD Model, Benefits, and Development Process.pdf
What is SaMD Model, Benefits, and Development Process.pdf
 
Unlocking the Potential of AI in Spring.pdf
Unlocking the Potential of AI in Spring.pdfUnlocking the Potential of AI in Spring.pdf
Unlocking the Potential of AI in Spring.pdf
 
Mobile Banking App Development Cost in 2024.pdf
Mobile Banking App Development Cost in 2024.pdfMobile Banking App Development Cost in 2024.pdf
Mobile Banking App Development Cost in 2024.pdf
 
Education App Development : Cost, Features and Example
Education App Development : Cost, Features and ExampleEducation App Development : Cost, Features and Example
Education App Development : Cost, Features and Example
 
Firebase Push Notification in JavaScript Apps
Firebase Push Notification in JavaScript AppsFirebase Push Notification in JavaScript Apps
Firebase Push Notification in JavaScript Apps
 
Micronaut Framework Guide Framework Basics and Fundamentals.pdf
Micronaut Framework Guide Framework Basics and Fundamentals.pdfMicronaut Framework Guide Framework Basics and Fundamentals.pdf
Micronaut Framework Guide Framework Basics and Fundamentals.pdf
 
Steps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MACSteps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MAC
 
Python Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txtPython Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txt
 
Gain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring BatchGain Proficiency in Batch Processing with Spring Batch
Gain Proficiency in Batch Processing with Spring Batch
 

Recently uploaded

Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 

Recently uploaded (20)

Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 

Spock-Framework: The Logical Choice for Modern Testing

  • 1. Spock-Framework: The Logical Choice for Modern Testing Introduction We'll look at Spock, a Groovy testing framework. Mainly, Spock aims to be a more powerful alternative to the traditional JUnit stack, by leveraging Groovy features. By making use of Groovy, Spock introduces new and expressive ways of testing our Java applications, which simply aren't possible in ordinary Java code. We'll explore some of Spock's high-level concepts during this article, with some practical step-by-step examples. Prerequisite You have some knowledge about spring-boot and testing concept like mocking, Stubbing, etc. Here we learn the Spock framework with the Spring boot application. To use Spock in spring boot, add the below dependency into pom.xml <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>2.3-groovy-4.0</version> <scope>test</scope>
  • 2. </dependency> <dependency> <groupId>org.apache.groovy</groupId> <artifactId>groovy</artifactId> <version>4.0.4</version> </dependency> And add a plugin for groovy support <plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.5</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> Simple test using Spock framework Make groovy class and extend the specification to it (spock.lang.specification). For testing we made one simple method.
  • 3. @SpringBootTest class Test1 extends Specification { def "Simple Test"(){ expect: 1+1 == 2 } } Here “def” keyword is used to define method after this you can add method discerption. Now run the test Output: You see Test Case Passed successfully.
  • 4. Controller Test-Cases First, create a ‘Hello’ controller @RestController public class Hello { @GetMapping("/hello") public String greeting (){ return "Hello World! "; } Now for this controller, we are writing test cases using Spock. class HelloTest extends Specification { @MockBean def cont = new Hello () MockMvc mvc = MockMvcBuilders.standaloneSetup(cont). build() def "when get is performed then the response has status 200 and content is 'Hello world!'"() { expect: "Status is 200 and the response is 'Hello world!'" mvc.perform(MockMvcRequestBuilders.get("/hello")) .andExpect(status().isOk()) .andReturn() .response .contentAsString == "Hello World! " } }
  • 5. Output: Keywords: 1. Expect: This is a way of performing our stimulus and assertion within the same block. Depending on what we find more expressive, we may or may not choose to use this block 2. When – This is where we provide a stimulus to what is under test. In other words, where we invoke our method under test 3. Then – This is where the assertions belong. In Spock, these are evaluated as plain Boolean assertions, which will be covered later
  • 6. Important Tips 1) All the blocks in a Spock based spec are optional. However, if present “when” and “then” should appear together in sequence. 2) If you just need to validate an assertion (and you don’t have a need of when block), you can use “expect” block. It can be used to assert pre-conditions even before when and then blocks (and can be used multiple times in a test). Originally published by: Spock Framework