0
0
Rest APIprogramming~20 mins

Contract testing in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Contract Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this contract test simulation?

Consider a REST API contract test where the consumer expects a JSON response with a userId as an integer and userName as a string. The provider returns the following JSON:

{"userId": "123", "userName": "Alice"}

What will the contract test most likely report?

ATest fails due to missing userName key
BTest passes because the keys match
CTest fails due to type mismatch on userId
DTest passes because values are convertible
Attempts:
2 left
💡 Hint

Check the expected data types in the contract.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes contract testing?

Choose the best description of contract testing in REST APIs.

ATesting the database schema behind the API
BVerifying that the API provider and consumer agree on request and response formats
CLoad testing the API under heavy traffic
DTesting the UI of the API consumer application
Attempts:
2 left
💡 Hint

Think about what contract testing ensures between two systems.

🔧 Debug
advanced
2:00remaining
Identify the cause of contract test failure

Given this contract test snippet for a REST API consumer:

expect(response).toHaveProperty('data.id');
expect(typeof response.data.id).toBe('number');
expect(response.data.name).toBeDefined();

The provider returns:

{"data": {"id": null, "name": "Bob"}}

Why does the contract test fail?

ABecause <code>data.id</code> is null, not a number
BBecause <code>data.name</code> is missing
CBecause <code>data</code> key is missing
DBecause the response is not JSON
Attempts:
2 left
💡 Hint

Check the expected type of data.id.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a JSON schema for contract testing?

Choose the valid JSON schema snippet that requires id as an integer and name as a string in the response object.

A{ "object": true, "props": { "id": { "type": "int" }, "name": { "type": "string" } }, "mandatory": ["id", "name"] }
B{ "type": "object", "properties": { "id": "integer", "name": "string" }, "required": ["id", "name"] }
C{ "type": "object", "fields": { "id": { "type": "int" }, "name": { "type": "str" } }, "required": ["id", "name"] }
D{ "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" } }, "required": ["id", "name"] }
Attempts:
2 left
💡 Hint

Look for correct JSON schema keywords and types.

🚀 Application
expert
2:30remaining
How many tests are needed to fully cover a contract with 3 optional and 2 required fields?

You have a REST API contract with 5 fields in the response: 2 are required and 3 are optional. To fully test all combinations of optional fields presence or absence along with required fields, how many distinct contract tests are needed?

A8
B5
C10
D32
Attempts:
2 left
💡 Hint

Think about combinations of optional fields only.