简体   繁体   English

如何在junit中使用属性文件测试参数

[英]how to test parameters using properties file in junit

I was preparing a test case scenario where I put the values to be tested against their keys in properties file. 我正在准备一个测试用例场景,在该场景中,将要针对它们的键进行测试的值放在properties文件中。 I wanted if all key values satisfied with their corresponding values, the method should return true, else false. 我想如果所有键值都满足其对应值,则该方法应返回true,否则返回false。

Below is my code: 下面是我的代码:

package mypackTest;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class Test123 {

    static Properties p = null;
    private boolean expected;

    @Parameters
    public static List<String[]> data() 
    {
      String[][] data = new String[][] { { "D:\\personal\\config.properties" }};
   return Arrays.asList(data);
    }


    @BeforeClass
    public static void setup()
    {  
          p = new Properties();
        try 
        {
           //load a properties file 
            p.load(new FileReader(new File("D:\\personal\\config.properties")));
        } 
        catch (IOException ex) 
        {
       ex.printStackTrace();
        }
}



    @Test
    public void do_test() {

        assertThat(TestThis.letstest(p.getProperty("abc1"), p.getProperty("abc2")), is(expected));
    }
}

the class that I wanted to test upon: 我要测试的课程:

package mypackTest;

public class TestThis {

    public static boolean letstest(String abc1,String abc2){
        if(abc1.equals("xyz1")&& abc2.equals("xyz2")){
            return true;
        }
        return false;
    }
}

properties file 属性文件

abc1=xyz1
abc2=xyz2

TestRunner class TestRunner类

package mypackTest;

import java.util.Enumeration;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

import junit.framework.TestFailure;
import junit.framework.TestResult;
import junit.framework.TestSuite;

public class TestRunner {
    public static void main(String[] args) {

              Result result = JUnitCore.runClasses(CalculatorTest.class);

              for (Failure failure : result.getFailures()) {
                 System.out.println(failure.toString());
              }

              System.out.println(result.wasSuccessful());
    }
}

I am getting below output 我的输出低于

OUTPUT OUTPUT

do_test0: wrong number of arguments do_test0:错误的参数数量

false

There are various problems with this code. 此代码存在各种问题。 First of all: 首先:

private boolean expected;

You are declaring that field - but you never assign a value to it. 您正在声明该字段-但您从未为其分配值。 Thus it is initialized to false and stays false . 因此,它被初始化为false并保持false So don't expect that it comparing it to something that is true would work out. 因此,不要期望将它与true事物进行比较会成功。

But beyond that: the way you are using Parametrized doesn't make any sense. 但除此之外:使用Parametrized方式没有任何意义。 The idea of this special runner is: the @Parameters "thing" contains multiple values; 这个特殊运行器的思想是:@Parameters“ thing”包含多个值; and your test is invoked in a loop for each entry. 然后为每个条目循环调用您的测试。 Having a single file name there - that is like "buying for a truck to then transport a single suitcase". 那里只有一个文件名-就像“购买卡车然后运送一个手提箱的文件名”。 Meaning: you would rather specify a list of key/value entries there; 含义:您宁愿在此处指定键/值条目列表; and then (for example) make sure that a property file contains all these entries. 然后(例如)确保属性文件包含所有这些条目。 Or something like that. 或类似的东西。

So the real answer is: step back and learn about the basics. 因此,真正的答案是:退后一步,了解基础知识。 For example your usage of static is also implying that you have no understanding of what static actually is, and when to use it. 例如,您对static的使用还意味着您不了解什么是static ,以及何时使用它。 And then follow a tutorial for JUnit (normal JUnit). 然后遵循有关JUnit(普通JUnit)的教程。 And when you figured how to use that properly, then read a tutorial about @Parametrized. 当您确定如何正确使用它时,请阅读有关@Parametrized的教程。

In other words: don't apply "trial and error" when you are lacking basic understanding. 换句话说:当您缺乏基本的了解时,请不要应用“试错法”。 This strategy only works when you have enough experience to not always error immediately for anything you try. 当您有足够的经验而不总是立即为尝试的任何操作出错时,此策略起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM