DEV Community

Edgar Rios Navarro
Edgar Rios Navarro

Posted on

2 2

Mock Service with Groovy

Cuando dependemos de una servicio de tercero, nos encontraremos con la situación que dicho servicio no está disponible por un tiempo. Para eliminar esa dependencia, podemos crear un MockService por nuestra cuenta.

En el ecosistema de Groovy, existe una librería que nos facilita: RatPack.


En una solo archivo, configuramos los métodos y las respuestas. Es posible agregarle lógica, pero la intención es que tengamos respuesta.

@Grab("io.ratpack:ratpack-groovy:1.3.3")
import static ratpack.groovy.Groovy.ratpack
ratpack {
  handlers {
    path("Transaccion.asmx/getAffiliationSearch") {
      byMethod {
        get {
          response.contentType "text/xml"
          response.send '''<?xml version="1.0" encoding="utf-8"?>
            <ArrayOfStAffiliationSearch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://mifarmaws02.orbisfarma.com.mx/">
                <stAffiliationSearch>
                    <transactionid>183477976581</transactionid>
                    <cardnumber>D007330811</cardnumber>
                    <accountnumber>D007330811</accountnumber>
                    <cardname>VILMA ALEJANDRINA</cardname>
                    <cardlastname>FERNANDEZ@RUIZ</cardlastname>
                    <cardbirthdate>19641008</cardbirthdate>
                    <cardgender>F</cardgender>
                    <carddoctor />
                    <cardcontactpreferences>1</cardcontactpreferences>
                    <cardemail />
                    <cardphone />
                    <cardcellphone />
                    <cardaddress>| | | | | |</cardaddress>
                    <cardrelated>8601080650271</cardrelated>
                    <cardfields>0</cardfields>
                    <errorid>0</errorid>
                    <message />
                    <transactiondate>2022-11-24 18:00:00</transactiondate>
                </stAffiliationSearch>
            </ArrayOfStAffiliationSearch>'''

Enter fullscreen mode Exit fullscreen mode
groovy mock-service.groovy
Enter fullscreen mode Exit fullscreen mode

Al consultar:
Image description


Documentación

https://ratpack.io/

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay