Utilizando la inyección de la propiedad de la primavera sin tener que lanzar a todo el contexto

xetra11:

Quiero correr algunas pruebas de unidad sin la necesidad del contexto primavera, debido a razones de velocidad. Pero también me gusta la manera en que puedo inyectar archivos en mis pruebas

Este es un ejemplo testcode que utilizo:

@ContextConfiguration()
@SpringBootTest
@RunWith(SpringRunner.class)
public class XmlRestructureApplierTest {
  @Value("classpath:export.xml")
  private Resource exportedXML;
  private XmlRestructureApplier applier;


  @Test
  public void shouldRestructureArrayToObjectWithGivenKey() throws IOException, XPathExpressionException, SAXException {
    List<JSONObject> productsAsJSONObjects = XmlElementExtractor.extractToJSON(exportXML.getInputStream(), "PRV");
    assertThat(productsAsJSONObjects).hasSize(6);
  }
}

Me gustaría tener sólo la forma más conveniente de usar @Valuesin necesidad de iniciar todo el tiempo contexto.

¿Es posible esto de alguna manera?

i.bondarenko:

Se podría utilizar la configuración de ensayo vacío para tal prueba, se mejorará el rendimiento. En el ejemplo siguiente, @SpringBootTestúnica carga incrustado EmptyTestContexten lugar de buscar todos SpringBootConfiguration:

@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoMvcApplicationTest {
    @Value("${test.value}")
    private String value;

    @Test
    public void propertyHasValidValue() {
        assertThat(value).isEqualTo("TestValue1");
    }

    @Configuration
    public static class EmptyTestContext {}
}

También para más facilidad de lectura se podría añadir:

@SpringBootTest(classes = DemoMvcApplicationTest.EmptyTestContext.class)

Tiene el mismo efecto.

Actualización , la variante más ligera:

@RunWith(SpringRunner.class)
@ContextConfiguration
public class DemoMvcApplicationTest {
    @Value("${test.value}")
    private String value;

    @Test
    public void propertyHasValidValue() {
        assertThat(value).isEqualTo("TestValue1");
    }

    @Configuration
    @PropertySource("classpath:application.properties")
    public static class EmptyTestContext {}
}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=330213&siteId=1
Recomendado
Clasificación