【问题标题】:in eclipse/Junit with multiple tests how to retrieve initial default directory在具有多个测试的 eclipse/Junit 中如何检索初始默认目录
【发布时间】:2018-01-19 19:25:18
【问题描述】:

当我使用junit执行one测试时,我可以通过设置user.dir来找到当前目录,即开始目录下的文件,或者相对改变它。

但是当我启动多个测试时,user.dir 仍然存在于测试之间,如果我之前设置它,那就是一团糟。

每个测试如何获得初始默认目录,即使它被先前的测试更改,而不设置测试的每个配置(-Duser.dir ...)

感谢 GhostCat,这是一个简短的解决方案:

static String user_dir_initial="";  
@Before
       public void before()
        {
    if (user_dir_initial.contentEquals(""))
        user_dir_initial=System.getProperty("user.dir");
        System.out.println("USER DIR INITIAL:"+user_dir_initial);
        System.setProperty("user.dir", user_dir_initial);
        }

【问题讨论】:

    标签: java eclipse junit directory


    【解决方案1】:

    如果要确保为所有测试用例设置了某个属性,可以使用@Before 或@BeforeClass 注解结合System.setProperty()强制 某个设置。

    如果必须撤消,可以对称使用@After 或@AfterClass!

    【讨论】:

      【解决方案2】:

      如果您将user.dir 设置为系统属性,那么除非您将其删除,否则它将在该JVM 的生命周期内一直存在。因此,您必须为每个测试生成一个新的 JVM,或者以某种方式管理测试用例之间的系统属性。您可以使用JUnit Rule 轻松设置/取消设置系统属性。

      这是一个例子:

      public class SystemPropertyRule extends ExternalResource {
          private final Map<String, String> properties = new LinkedHashMap<String, String>();
          private final Map<String, String> restoreProperties = new LinkedHashMap<String, String>();
      
          public SystemPropertyRule(String propertyName, String propertyValue) {
              this.properties.put(propertyName, propertyValue);
          }
      
          @Override
          protected void before() throws Throwable {
              for (Map.Entry<String, String> entry : properties.entrySet()) {
                  String propertyName = entry.getKey();
                  String propertyValue = entry.getValue();
                  String existingValue = System.getProperty(propertyName);
                  if (existingValue != null) {
                      // store the overriden value so that it can be reinstated when the test completes
                      restoreProperties.put(propertyName, existingValue);
                  }
                  System.setProperty(propertyName, propertyValue);
              }
          }
      
      
          @Override
          protected void after() {
              for (Map.Entry<String, String> entry : properties.entrySet()) {
                  String propertyName = entry.getKey();
                  String propertyValue = entry.getValue();
                  if (restoreProperties.containsKey(propertyName)) {
                      // reinstate the overridden value
                      System.setProperty(propertyName, propertyValue);
                  } else {
                      // remove the (previously unset) property
                      System.clearProperty(propertyName);
                  }
              }
          }
      }
      

      你可以像这样在你的测试用例中使用它:

      @ClassRule
      public static final SystemPropertyRule systemPropertyRule = new SystemPropertyRule("foo", "bar");
      
      @Test
      public void testPropertyIsSet() {
          Assert.assertEquals("bar", System.getProperty("foo"));
      }
      

      此规则将包装测试用例调用,并添加以下行为:

      • 之前:使用给定值设置命名系统属性
      • 之后:丢弃该属性(如果在运行此测试用例之前未设置)或恢复该属性的先前值(如果该属性在运行测试用例之前有值)

      使用此规则,您可以控制为每个测试设置 user.dir(允许对其进行设置/取消设置/恢复等),尽管它最终确实相当于为每个测试调用 user.dir=...,但它不是很具有侵入性,并且它使用了专门用于此目的的 JUnit 机制。

      【讨论】:

        猜你喜欢
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 2010-09-12
        • 2017-08-14
        • 1970-01-01
        • 2011-07-27
        • 2020-04-08
        • 1970-01-01
        相关资源
        最近更新 更多