DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Error Handling Inside Kumologica Subflow
  • How to Test QR Codes in Your Applications
  • Refining Automated Testing: Balancing Speed and Reliability in Modern Test Suites
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests

Trending

  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Munit: Parameterized Test Suite

Munit: Parameterized Test Suite

This article explains the usage of parameterized test suites using Munit tests to validate every request field present in the request payload.

By 
Ankur Bhuyan user avatar
Ankur Bhuyan
·
Feb. 07, 24 · Code Snippet
Likes (28)
Comment
Save
Tweet
Share
3.0K Views

Join the DZone community and get the full member experience.

Join For Free

The purpose of this use case is to explain how to define different RAML data types, define the business-related status code for request payload validation, define the single unit test case for multiple field validation with dynamic request payload, and how to use the parameterized test suite. 

With a parameterized test suite, we can write a reusable unit test case. It can help to write and test multiple scenarios based on different inputs with a single test case. This can be more useful when we are writing a test case to validate the request and response payload.

RAML Data Types Definition

Define different types of data types for a request payload. 

In the below example, I covered integer, string, pattern, enum, array, object, datetime, datetime-only, and time-only.

YAML
 
#%RAML 1.0 DataType

type: object
properties:
  employeeId: 
    type: integer
    required: true
    minimum: 8
  firstName: 
    type: string
    required: true
    minLength: 1
    maxLength: 10
    pattern: ^[A-Za-z]*
  lastName: 
    type: string
    required: true
    minLength: 1
    maxLength: 10
    pattern: ^[A-Za-z]*
  email:
    pattern: ^.+@.+\..+$
  gender:
    enum: [male, female]
    default: male
    required: true
  dateOfBirh:
    type: date-only
    required: true 
  addresses:
    type: array
    minItems: 1
    items:
      type: object
      properties:
        isPermanent:
          type: boolean
          required: true
        street:
          type: string
          required: true
          minLength: 5
          maxLength: 50
          pattern: ^[A-Za-z ]*
        district:
          type: string
          required: true
          minLength: 3
          maxLength: 20
          pattern: ^[A-Za-z]*
        state:
          type: string
          required: true
          minLength: 5
          maxLength: 15
          pattern: ^[A-Za-z]*
        pinNumber:
          type: integer
          required: true
          minimum: 6
        province:
          type: string
          required: true
          minLength: 1
          maxLength: 10
          pattern: ^[A-Za-z]*
        phoneNumber:
          type: string
          required: true
          minLength: 1
          maxLength: 13
          pattern: ^\s*|^(0|91)?[6-9][0-9]{9}$
  created:
    type: datetime
    format: rfc3339
    required: true
  createdDateTime:
    type: datetime-only
    required: true
  createdTime:
    type: time-only
    required: true


Sample Request Payload

Based on the RAML definition, prepare a valid request payload.

JSON
 
{
    "employeeId": 12345678,
    "firstName": "Ankur",
    "lastName": "Bhuyan",
    "email": "ankur.bhuyan@gmail.com",
    "gender": "male",
    "dateOfBirh": "2000-04-01",
    "addresses": [
        {
            "isPermanent": true,
            "street": "teachers colony",
            "district": "Sunitpur",
            "state": "Assam",
            "pinNumber": 784507,
            "province": "Tezpur",
            "phoneNumber": "+919590951234"
        }
    ],
    "created": "2016-02-28T12:30:00.090Z",
    "createdDateTime": "2016-02-28T12:30:00",
    "createdTime": "12:30:00"
}


Configure Parameterized Test Suite for Valid Scenarios

This is a valid test case scenario for which the parameterized inputs are defined. Based on the parameterization "name" defined, the test result name will be prepared (once the test case is executed). We have defined only one property with which the input payload for the test case will be prepared, which will vary based on the test scenario we will cover.

XML
 
<munit:config name="apikit-valid-test-suite.xml" >
	<munit:parameterizations >
		<munit:parameterization name="001" >
			<munit:parameters >
				<munit:parameter propertyName="caseNumber" value="001" />
			</munit:parameters>
		</munit:parameterization>
	</munit:parameterizations>
</munit:config>


Configure Parameterized Test Suite for Invalid Scenarios

This is an invalid test case scenario for which the parameterized inputs are defined. 

We can add as many numbers of the parameter (like caseNumber, expectedType, expectedCode) based on the use case. This is an example to show how we can define multiple parameters for a single test case.

XML
 
<munit:config name="apikit-invalid-test-suite.xml">
	<munit:parameterizations >
		<munit:parameterization name="001" >
			<munit:parameters >
				<munit:parameter propertyName="caseNumber" value="001" />
				<munit:parameter propertyName="expectedType" value="REQUIRED_KEY" />
				<munit:parameter propertyName="expectedCode" value="ANK000001" />
			</munit:parameters>
		</munit:parameterization>
		<munit:parameterization name="002" >
			<munit:parameters >
				<munit:parameter propertyName="caseNumber" value="002" />
				<munit:parameter propertyName="expectedType" value="TYPE" />
				<munit:parameter propertyName="expectedCode" value="ANK000002" />
			</munit:parameters>
		</munit:parameterization>
      <!-- define all posible test parameters -->
	</munit:parameterizations>
</munit:config>


Dynamic Payload for Munit Test

This is how we can write a test case with a dynamic request payload. This will help to define a number of different input payloads based on the test scenario that can be used as a parameterized input.

XML
 
<ee:transform doc:name="payload" doc:id="7c934c10-d874-4207-be27-de6c8b1a1c5a" >
	<ee:message >
		<ee:set-payload >
			<![CDATA[%dw 2.0
output application/json
var fileBaseName = "-invalid-payload.json"
var caseNumber = Mule::p("caseNumber")
var fileName = caseNumber ++ fileBaseName
---
readUrl("classpath://test-data/employee/payload/$(fileName)", "application/json")]]>
		</ee:set-payload>
	</ee:message>
</ee:transform>


Test Output

We can test multiple field validation with a single munit test by using dynamic payload and parameterized input.

Test Output


Code Reference

To find more on the above description, please follow the full code reference here.

Test case Test suite Testing Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Error Handling Inside Kumologica Subflow
  • How to Test QR Codes in Your Applications
  • Refining Automated Testing: Balancing Speed and Reliability in Modern Test Suites
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!