【问题标题】:Make resource xml configurable, while make tests in java?使资源xml可配置,同时在java中进行测试?
【发布时间】:2016-02-01 09:36:45
【问题描述】:

我必须测试 100 多种不同的情况,并且对于每一种情况,我都需要读取和解析的外部 xml。 我用:

String xml = IOUtils.toString(
                this.getClass().getResourceAsStream(path),encoding);

例如我的测试xml:

<container xmlns:dmc="http://example.com/common">
    <object id="1369" checkedParamter="in" class="Class1">
...

</object>
</container>

但我必须使用 ivalid id 进行测试,缺少 id 和现有 id。然后我需要 checkedParamter 有 3-4 个值并将所有组合与 id 属性结合起来。对于现在的每个测试,我都会创建新的 xml,唯一的区别是这两个属性 idcheckedParamter。 我想知道是否有简单的方法来读取 xml 并使用相同的结构,但要从我的测试中传递这些值。

 <container xmlns:dmc=" http://example.com/common">
        <object id= ${valueId} checkedParamter=${valueChechedParamter} class="Class1">
    ...

    </object>
    </container>

然后我将使用一个 xml 并将期望值放在测试的开头。我没有技术或方法吗?

【问题讨论】:

    标签: java xml testing junit apache-commons-io


    【解决方案1】:

    最好的方法是使用 ${valueId} 的单独文件,就像您已经拥有的那样。

    我们将使用 JUnit 的以下特性来实现我们的目标:

    我们将以下文件存储到我们项目的resources 部分:

    <container xmlns:dmc=" http://example.com/common">
        <object id= ${valueId} checkedParamter=${valueChechedParamter} class="Class1">
            ...
        </object>
    </container>
    

    然后我们开始测试:

    @RunWith(Parameterized.class)
    public class XmlInputTest {
    
    @Parameters
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][] {
                     { 1369, "in" }, 
                     { 1369, "out" }, 
                     { 753, "in" }, 
                     // etc.... 
               });
        }
    
    
    
    @Parameter(value = 0)
    public int id;
    
    @Parameter(value = 1)
    public String checkedParamter;
    
    @Test
    public void mainTest() {
        String xml = IOUtils.toString(
             this.getClass().getResourceAsStream("template.xml"),encoding);
        xml = xml.replace("${valueId}",String.valueOf(id)).replace("${valueChechedParamter}",checkedParamter);
    
        // remaing test....
    }
    }
    

    使用这种测试运行方法的优点是您有一个简单的输入列表进行测试。

    【讨论】:

    • 太棒了,你为我节省了 10 个小时的 xmling。但我想让抽象更高一级。要指定或不指定整个属性,所以 ${id=value},而不仅仅是值。如果我这样做,则 xml 无效。有没有办法解决这个问题?
    • 然后您可以做的是在 xml 解析器中加载 xml,并在此基础上添加/删除属性。您可以添加在主测试运行之前运行的@Before 测试方法,以在测试范围之外创建 xml。
    【解决方案2】:

    您可以在测试开始时尝试这样的方法。

    Map<String,String> properties = new HashMap<String, String>();
    properties.put("valueId", "1");
    properties.put("valueChechedParamter", "0");
    
    String propertyRegex = "\\$\\{([^}]*)\\}";
    Pattern pattern = Pattern.compile(propertyRegex);
    
    int i = 0;
    Matcher matcher = pattern.matcher(xml);
    StringBuilder result = new StringBuilder(xml.length());
    while(matcher.find()) {
        result.append(expression.substring(i, matcher.start()));
        String property = matcher.group();
        property = property.substring(2, property.length() - 1);
        if(properties.containsKey(property)) {
            property = properties.get(property);
        } else {
            property = matcher.group();
        }
        result.append(property);
        i = matcher.end();
    }
    
    result.append(expression.substring(i));
    String resultXml = result.toString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2018-12-23
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多