Diferencia entre revisiones de «Mock de un WS mediante Spring WS»

De Dos Ideas.
Saltar a: navegación, buscar
Línea 44: Línea 44:
 
</code>
 
</code>
  
=Fuente=  
+
====Fuente=====
 
*[http://static.springsource.org/spring-ws/site/reference/html/client.html Spring MVC Test ]
 
*[http://static.springsource.org/spring-ws/site/reference/html/client.html Spring MVC Test ]

Revisión del 17:18 29 mar 2012

Introducción

En esta página explicaremos de qué forma podemos simular el entorno de un WS en memoria, de modo tal de poder realizar un test de componentes contra dicho entorno.

Requerimientos

Para mockear un WS mediante Spring WS, necesitaremos agregar a nuestro proyecto los siguientes jars:

  • jaxb-api-2.0.jar
  • jaxb-impl-2.0.3.jar
  • xalan-2.7.1.jar (importante que sea esta versión o superior)
  • xmlunit-1.1.jar

Ejemplo

Básicamente, el código para crear un test contra dicho entorno será como el siguiente:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("profile-service-client.xml") public class ProfileServiceClientIntegrationTest {

private String request = "<ns2:UserProfileCreateRequest xmlns:ns2='http://shekhar.com/usermanagement/schemas'> Shekhar Gulati 27 "; private String response = "<ns2:UserProfileCreateResponse xmlns:ns2='http://shekhar.com/usermanagement/schemas'> user created successfully ";

@Autowired private WebServiceTemplate webServiceTemplate; private MockWebServiceServer server; @Autowired private ProfileServiceClient client;

@Before public void setup() { server = MockWebServiceServer.createServer(webServiceTemplate);//1 }

@Test public void testInvokeProfileServiceAndGetASuccessResponse() { server.expect(RequestMatchers.payload(new StringSource(request))).andRespond(ResponseCreators.withPayload(new StringSource(response)));//2 Assert.assertEquals("user created successfully",client.invokeProfileServiceAndGetASuccessResponse());//3 server.verify();//4 }

}

Fuente=