DEV Community

Cover image for Microcks for dummies
Supervinh for opt-nc

Posted on

Microcks for dummies

๐Ÿค” Microcks: For Whom and For What?

How can you tell if API documentation:

  • Is properly documented ?
  • Is sufficient for effective usage ?
  • Allows partners/clients to efficiently test ?

This is exactly what Microcks, an open-source tool, promises to deliver. Its benefits have already been explored in one of OPT-NCโ€™s public APIs for agency waiting times.

๐Ÿค“ Spoilers

๐Ÿ“œ Annotations and REST API Documentation Quality

Why document a REST API using annotations directly in the source code?

  • ๐Ÿ’ก Clarity for developers: Annotations precisely describe endpoints, parameters, and responses, making the API easier to understand.
  • ๐Ÿ”„ Automatic updates: Generating OpenAPI documentation from annotations ensures that it stays aligned with the source code in a simple and efficient way.
  • ๐Ÿ“š Concrete examples: Annotations also allow adding example calls, helping developers understand practical use cases.

Hereโ€™s an example of annotations:

@GET
@Path("/endpoint")
@Operation(
        summary = "Operation summary",
        description = "Detailed description"
)
@APIResponses(
        value = {
                @APIResponse(
                        responseCode = "200",
                        description = "OK response",
                        content = @Content(
                                mediaType = "application/json",
                                schema = @Schema(implementation = MyClass.class),
                                examples = {
                                        @ExampleObject(
                                                name = "Example 1",
                                                value = "{'key':'value'}"
                                        )
                                }
                        )
                )
        }
)
public ReturnValue myFunction() {}
Enter fullscreen mode Exit fullscreen mode

And to describe request parameters:

@GET
@Path("/endpoint/{id}")
public ReturnValue myFunction(
        @Parameter(
                description = "Expected ID",
                schema = @Schema(type = "integer"),
                examples = {
                        @ExampleObject(name = "Example ID", value = "123")
                })
        @PathParam("id") int id
) {}

Enter fullscreen mode Exit fullscreen mode

Validation and Security with Swagger:

  • โœ… Validation of parameters and responses.
  • ๐Ÿ”’ Endpoint security to prevent injection attacks.
  • ๐Ÿšซ Rejection of invalid values (e.g., negative numbers for non-allowed parameters).

But documentation - and hence its quality - must also be tested!

๐Ÿ’ผ Enter Microcks: This open-source tool generates mocks from OpenAPI annotations, enabling:

  • ๐ŸŒ API testing without actual deployment.
  • ๐Ÿงช Evaluation of documentation quality and completeness.
  • ๐Ÿ“Š Scoring for OpenAPI compliance to ensure high standards.

With Microcks, documenting is not just writing, it ensures everything works as expected, leading to robust, well-documented APIs. -> ...that can be embedded as part of a CI

๐Ÿš€ Microcks: Boosting Developer Experience

Thanks to annotations and the mocks generated by Microcks:

  • ๐Ÿ“ Clarity even before development: API functionalities are defined upfront, making communication between developers, test managers, project managers and future users easier.
  • โฑ๏ธ Time-saving: Development and documentation can progress in parallel, optimizing project resources, allowing those depending on the API to start development even before the API itself is implemented.

Microcks transforms how APIs are designed, tested, and shared, creating a seamless and efficient DEVEX (developer experience).

๐Ÿ” Microcks and the Testing Pyramid

Among the various representations of testing strategies, Watirmelonโ€™s model stands out as particularly insightful.

๐Ÿงฉ Microcks fits into the Automated API Tests layer, with a particular focus on Contract Testing, ensuring that APIs adhere to their specifications.

Ideal Software Testing Pyramide

๐ŸŽฏ Personal and team benefits

  • โœ… Documentation quality: I was pleasantly surprised by the clarity and precision of the documentation provided by Microcks.
  • ๐ŸŽจ Intuitive user interface: The UI is simple, intuitive, and very enjoyable to use, making the tool accessible right from the start.
  • โšก Developer responsiveness: The contributors of Microcks are highly active on GitHub and respond quickly to issues, which is reassuring when resolving potential roadblocks.
  • ๐Ÿš€ Unblocking my tests: In my last project, I struggled to mock calls to OpenSearch. With Microcks, I no longer need to mock the calls. I can simply simulate the database to contact, which saves time and greatly simplifies testing.
  • ๐Ÿ”ฎ Usefulness for future projects: Microcks will be invaluable for my future projects, especially during the development phases. Instead of relying on paid APIs, I can simply use my Microcks mock, which is both more economical and practical.

Microcks has proven to be a powerful ally, not only for this project but also for those to come.

Top comments (3)

Collapse
 
adriens profile image
adriens

Great job @supervinh et welcome at optnc ๐Ÿ‘

Collapse
 
adriens profile image
adriens

Let's become adopters <3

๐Ÿ๏ธ Microcks adopted by OPT-NC ๐Ÿฅณ #1460

โ” About

Since a few months we started to work on microcks, and now we feel confident on the fact that we add it to our stack, next to Quarkus.

So to celebrate this adoption, we cooked you some gifts :

Other contents will follow as well as evangelisation ๐Ÿ™Œ

Thanks a lot for this great tool and community feedbacks ๐Ÿ™

Collapse
 
adriens profile image
adriens