Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写Junit测试,使用TransformerFactory在XML上应用XSLT_Java_Xml_Xslt_Junit - Fatal编程技术网

Java 编写Junit测试,使用TransformerFactory在XML上应用XSLT

Java 编写Junit测试,使用TransformerFactory在XML上应用XSLT,java,xml,xslt,junit,Java,Xml,Xslt,Junit,我是Junit测试新手,我有一个xml文件和xslt文件,如下所示: File.xml <people> <person> <role>Parent</role> <lastname>Smith</lastname> <locations> <location> <city>Springfield</city>

我是Junit测试新手,我有一个xml文件和xslt文件,如下所示:

File.xml

<people>
<person>
    <role>Parent</role>
    <lastname>Smith</lastname>
    <locations>
        <location>
            <city>Springfield</city>
            <state>MA</state>
            <unimportant-field>123</unimportant-field>
        </location>
        <location>
            <city>Chicago</city>
            <state>IL</state>
            <unimportant-field>456</unimportant-field>
        </location>
    </locations>
</person>
<person>
    <role>Child</role>
    <lastname>Smith</lastname>
    <locations>
        <location>
            <city>Springfield</city>
            <state>IL</state>
            <unimportant-field>789</unimportant-field>
        </location>
        <location>
            <city>Chicago</city>
            <state>IL</state>
            <unimportant-field>000</unimportant-field>
        </location>
        <location>
            <city>Washington</city>
            <state>DC</state>
            <unimportant-field>555</unimportant-field>
        </location>
    </locations>
</person>

如果有人能指导我的话,我不知道如何为上述内容编写Junit测试。我将不胜感激。谢谢。

首先,最建议您编写Junit测试来测试API实用程序。我的意思是,您应该首先编写一个完整的语义抽象:一个具有正确命名的方法、参数和返回值的类。标准的main方法没有任何语义:您无法知道它做什么,它的名称没有特定的含义,它需要多少参数,也不知道它在哪里产生结果

所以,当你有这样一个类:

public class MyTransformer 
{
    public void transform(InputStream in, OutputStream out) 
    throws IOException, TransformException
    {...}
}
// If needed, You could also add a main method which delegates over the transform method.
public class MyTransformerTest
{
    // Transforms an existing file producing the result in memory,
    // and compares it with an existing, expected output file.
    private void test(String inputFilename, String expectedOutputFilename)
        throws IOException
    {
        try
        {
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            new MyTransformer().transform(new FileInputStream(inputFilename), out);
            String expectedResult=readFileAsString(expectedOutputFilename);
            assertEquals("File '" + inputFilename + "' was not transformed OK", expectedResult, out.toString("ISO-8859-1"));
        }
        catch (TransformerException e)
        {
            e.printStackTrace();
            fail(e.toString());
        }
    }

    @Test
    public void testEmpty()
        throws IOException
    {
        test("empty-input.xml", "empty-output.xml");
    }

    @Test
    public void testOnePersons()
        throws IOException
    {
        test("one-persons-input.xml", "one-persons-output.xml");
    }

    @Test
    public void testTwoPersons()
        throws IOException
    {
        test("two-persons-input.xml", "two-persons-output.xml");
    }
}
。。。您可以这样编写Junit测试仪:

public class MyTransformer 
{
    public void transform(InputStream in, OutputStream out) 
    throws IOException, TransformException
    {...}
}
// If needed, You could also add a main method which delegates over the transform method.
public class MyTransformerTest
{
    // Transforms an existing file producing the result in memory,
    // and compares it with an existing, expected output file.
    private void test(String inputFilename, String expectedOutputFilename)
        throws IOException
    {
        try
        {
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            new MyTransformer().transform(new FileInputStream(inputFilename), out);
            String expectedResult=readFileAsString(expectedOutputFilename);
            assertEquals("File '" + inputFilename + "' was not transformed OK", expectedResult, out.toString("ISO-8859-1"));
        }
        catch (TransformerException e)
        {
            e.printStackTrace();
            fail(e.toString());
        }
    }

    @Test
    public void testEmpty()
        throws IOException
    {
        test("empty-input.xml", "empty-output.xml");
    }

    @Test
    public void testOnePersons()
        throws IOException
    {
        test("one-persons-input.xml", "one-persons-output.xml");
    }

    @Test
    public void testTwoPersons()
        throws IOException
    {
        test("two-persons-input.xml", "two-persons-output.xml");
    }
}
并不是说该测试仪基于测试任何文件的通用测试方法。因此,您只需为您感兴趣的每个案例编写一个输入文件和一个预期的输出文件

这项技术将简化测试仪的未来维护:请记住,每次发现bug时,首先您必须在测试仪中添加一个方法来重现最初会失败的错误。然后修复代码,再次运行测试仪,直到它没有失败


还有一个注意事项:我通常在throws子句中保留IOExceptions,而不捕获它们。这是因为此异常不是代码的错误。所以,你的测试并不有趣。如果在测试人员执行时出现IOException,则表示无法实例化任何输入或输出文件:它发生在代码执行之前。

您是否与此相关?您似乎有相同的XML+XSLT:@michael.hor257k我在搜索堆栈溢出时复制了它。@michael.hor257k您能指导我如何为process方法编写测试用例吗。谢谢。我已经把那个主类做成了一个方法,你怎么能为process方法@little santi编写一个单元测试呢