Suppose you need to test the functions of the calculator. In the usual case, the input in the tests would be given the usual numbers. And what if you need to give an xml file as input?
2 answers
You place the test xml file as a resource in the tests and pass it to the test method. The file can be passed as 'inputStream' or as text.
|
The question is not completely clear, but I will try to answer, because in my tests I also use information from external files. Below is an example of code that in each test method provides access to an XML document.
package ru.test; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; /** * Тестовое приложение, которые использует в тестах данные из XML документа. */ public class TestApp { private Document document; /** * Метод, который будет вызываться перед каждым тестом в файле. * Загружает и парсит необходимый XML документ. */ @Before public void setUp() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // Здесь можно указывать любой файл в classpath и не только. document = builder.parse(new File("src_test/test.xml")); } /** * Метод, который вызывается после каждого теста. * Здесь можно ничего не делать. */ @After public void tearDown() { document = null; } /** * Сам тест. * Здесь получаем какую-то очень нужную информацию из XML документа. */ @Test public void testDocument() { // Запрашиваем элементы data из XML документа. document.getElementsByTagName("data"); } } In the testDocument method, you can select data from the already loaded XML document.
|