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 @Value
sin 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 EmptyTestContext
en 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 {}
}