PageSourceSearch

https://serenity-bdd.github.io/es/assets/js/f366ea59.bf718ace.js

js serenity-bdd.github.io collected 2026-10-03 09:57:57 UTC 9,043 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkserenity_users_guide=globalThis.webpackChunkserenity_users_guide||[]).push([[1306],{8453(e,n,i){i.d(n,{R:()=>o,x:()=>s});var a=i(6540);const t={},r=a.createContext(t);function o(e){const n=a.useContext(r);return a.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function s(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:o(e.components),a.createElement(r.Provider,{value:n},e.children)}},8558(e,n,i){i.r(n),i.d(n,{assets:()=>c,contentTitle:()=>s,default:()=>u,frontMatter:()=>o,metadata:()=>a,toc:()=>d});const a=JSON.parse('{"id":"spring/integration_testing_spring","title":"Pruebas de integracion con Spring","description":"Los proyectos Spring son muy adecuados para ejecutar pruebas de aceptacion contra diferentes capas de tu aplicacion.","source":"@site/i18n/es/docusaurus-plugin-content-docs/current/spring/integration-testing-spring.md","sourceDirName":"spring","slug":"/spring/integration_testing_spring","permalink":"/es/docs/spring/integration_testing_spring","draft":false,"unlisted":false,"editUrl":"https://github.com/serenity-bdd/serenity-bdd.github.io/blob/master/docs/spring/integration-testing-spring.md","tags":[],"version":"current","sidebarPosition":1,"frontMatter":{"id":"integration_testing_spring","title":"Pruebas de integracion con Spring","sidebar_position":1},"sidebar":"tutorialSidebar","previous":{"title":"Integracion con Cucumber","permalink":"/es/docs/playwright/playwright_cucumber"},"next":{"title":"Los Reportes de Serenity","permalink":"/es/docs/reporting/the_serenity_reports"}}');var t=i(4848),r=i(8453);const o={id:"integration_testing_spring",title:"Pruebas de integracion con Spring",sidebar_position:1},s=void 0,c={},d=[{value:"En Spring Boot &lt;1.3",id:"en-spring-boot-13",level:3}];function l(e){const n={code:"code",h3:"h3",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.p,{children:"Los proyectos Spring son muy adecuados para ejecutar pruebas de aceptacion contra diferentes capas de tu aplicacion."}),"\n",(0,t.jsx)(n.p,{children:"Con Serenity, puedes asegurar el comportamiento correcto usando un navegador o la API REST, o trabajar directamente con la capa de servicios de tu aplicacion."}),"\n",(0,t.jsx)(n.p,{children:"Ocasionalmente tambien puede ser util acceder a las capas de servicio directamente en tus pruebas end-to-end basadas en navegador."}),"\n",(0,t.jsx)(n.p,{children:"Por ejemplo, puedes tener un escenario donde una accion del usuario debe, como efecto secundario, registrar un log de auditoria en una tabla de la base de datos."}),"\n",(0,t.jsx)(n.p,{children:"Para mantener tu prueba enfocada y simple, puedes querer llamar directamente a la capa de servicios para verificar los logs de auditoria, en lugar de usar el navegador para iniciar sesion como administrador y navegar a la pantalla de logs de auditoria."}),"\n",(0,t.jsxs)(n.p,{children:["Las pruebas de integracion deben usar el ",(0,t.jsx)(n.code,{children:"SerenityRunner"}),".\nPara poder inyectar (por ejemplo, ",(0,t.jsx)(n.code,{children:"@Autowired"}),") dependencias y usar las anotaciones habituales (como ",(0,t.jsx)(n.code,{children:"@DirtiesContext"}),"), solo necesitas incluir la regla de Serenity ",(0,t.jsx)(n.code,{children:"SpringIntegrationMethodRule"})," en tu clase de prueba."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:'@RunWith(SerenityRunner.class)\n@SpringBootTest\npublic class WhenInjectingSpringDependencies {\n\n    @Managed\n    WebDriver driver;\n\n    @ManagedPages(defaultUrl = "http://www.google.com")\n    public Pages pages;\n\n    @Rule\n    public SpringIntegrationMethodRule springIntegrationMethodRule =\n      new SpringIntegrationMethodRule();\n\n    @Autowired\n    public GizmoService gizmoService;\n\n    @Test\n    public void shouldInstantiateGizmoService() {\n        assertThat(gizmoService, is(not(nullValue())));\n    }\n\n    @Test\n    public void shouldInstantiateNestedServices() {\n        assertThat(gizmoService.getWidgetService(), is(not(nullValue())));\n    }\n}\n'})}),"\n",(0,t.jsx)(n.h3,{id:"en-spring-boot-13",children:"En Spring Boot <1.3"}),"\n",(0,t.jsxs)(n.p,{children:["Usa la anotacion ",(0,t.jsx)(n.code,{children:"@ContextConfiguration"})," para definir el archivo o archivos de configuracion a usar. Luego puedes inyectar dependencias como lo harias con una prueba de integracion Spring ordinaria, usando las anotaciones habituales de Spring como ",(0,t.jsx)(n.code,{children:"@Autowired"})," o ",(0,t.jsx)(n.code,{children:"@Resource"}),". Por ejemplo, supongamos que estamos usando el siguiente archivo de configuracion Spring, llamado 'config.xml':"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-xml",children:'<beans>\n    <bean id="widgetService" class="net.serenity.junit.spring.WidgetService">\n        <property name="name"><value>Widgets</value></property>\n        <property name="quota"><value>1</value></property>\n    </bean>\n    <bean id="gizmoService" class="net.serenity.junit.spring.GizmoService">\n        <property name="name"><value>Gizmos</value></property>\n        <property name="widgetService"><ref bean="widgetService" /></property>\n    </bean>\n</beans>\n'})}),"\n",(0,t.jsx)(n.p,{children:"Podemos usar este archivo de configuracion para inyectar dependencias como se muestra aqui:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:'@RunWith(SerenityRunner.class)\n@ContextConfiguration(locations = "/config.xml")\npublic class WhenInjectingSpringDependencies {\n\n    @Managed\n    WebDriver driver;\n\n    @ManagedPages(defaultUrl = "http://www.google.com")\n    public Pages pages;\n\n    @Rule\n    public SpringIntegrationMethodRule springIntegrationMethodRule = new SpringIntegrationMethodRule();\n\n    @Autowired\n    public GizmoService gizmoService;\n\n    @Test\n    public void shouldInstantiateGizmoService() {\n        assertThat(gizmoService, is(not(nullValue())));\n    }\n\n    @Test\n    public void shouldInstantiateNestedServices() {\n        assertThat(gizmoService.getWidgetService(), is(not(nullValue())));\n    }\n}\n'})}),"\n",(0,t.jsxs)(n.p,{children:["Otras anotaciones relacionadas con el contexto como ",(0,t.jsx)(n.code,{children:"@DirtiesContext"})," tambien funcionaran como lo harian en una prueba de integracion Spring tradicional. Spring creara un nuevo ApplicationContext para cada prueba, pero usara un unico ApplicationContext para todos los metodos en tu prueba. Si una de tus pruebas modifica un objeto en el ApplicationContext, puedes querer indicarselo a Spring para que pueda reiniciar el contexto para la siguiente prueba. Haces esto usando la anotacion ",(0,t.jsx)(n.code,{children:"@DirtiesContext"}),". En el siguiente caso de prueba, por ejemplo, las pruebas fallaran sin la anotacion ",(0,t.jsx)(n.code,{children:"@DirtiesContext"}),":"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:'@RunWith(SerenityRunner.class)\n@ContextConfiguration(locations = "/spring/config.xml")\npublic class WhenWorkingWithDirtyContexts {\n\n    @Managed\n    WebDriver driver;\n\n    @ManagedPages(defaultUrl = "http://www.google.com")\n    public Pages pages;\n\n    @Rule\n    public SpringIntegration springIntegration = new SpringIntegration();\n\n    @Autowired\n    public GizmoService gizmoService;\n\n    @Test\n    @DirtiesContext\n    public void shouldNotBeAffectedByTheOtherTest() {\n        assertThat(gizmoService.getName(), is("Gizmos"));\n        gizmoService.setName("New Gizmos");\n    }\n\n    @Test\n    @DirtiesContext\n    public void shouldNotBeAffectedByTheOtherTestEither() {\n        assertThat(gizmoService.getName(), is("Gizmos"));\n        gizmoService.setName("New Gizmos");\n    }\n\n}\n'})}),"\n",(0,t.jsx)(n.p,{children:"Tambien puedes inyectar dependencias de Spring directamente en tus bibliotecas de pasos, para JUnit, Cucumber y JBehave, como se muestra en este ejemplo:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-java",children:'@ContextConfiguration(locations = "/spring/config.xml")\npublic class NestedSpringEnabledSteps {\n\n    @Autowired\n    public WidgetService widgetService;\n\n    private String widgetName;\n\n    @Steps\n    private NestedSteps nestedSteps;\n\n    @Given("I have a nested autowired Spring bean")\n    public void givenIHaveAnAutowiredSpringBean() {\n        assertThat(nestedSteps.widgetService, notNullValue());\n    }\n\n    @When("I use the nested bean")\n    public void whenIUseTheBean() {\n        widgetName = nestedSteps.widgetService.getName();\n    }\n\n    @Then("the nested bean should be instantiated")\n    public void thenItShouldBeInstantiated() {\n        assertThat(widgetName, is("Widgets"));\n    }\n\n}\n'})})]})}function u(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(l,{...e})}):l(e)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.