0

いくつかのクラスで使用されるいくつかのグローバル変数を設定する必要があります。

プロパティ ファイルから静的変数を割り当てるのに問題があります。

変数を呼び出す方法は次のようなものです: String url = WebdriverConfiguration.getBaseUrl();

public class WebDriverConfiguration
{
    private static Properties testProperties;
    private static String instaceUrl;

    testProperties = loadProperties();


    public static final String DEFAULT_BASEURL = testProperties.getProperty("confluence.base.url","");
    private static final int DEFAULT_HTTP_PORT = 8080;
    private static final String DEFAULT_CONTEXT_PATH = "/";
    public static final String TEST_SPACE_KEY = "SMOKE";
    public static final String TEST_PAGE = "XXX";

    private static final String BASE_URL = System.getProperty("baseurl", DEFAULT_BASEURL);


    public static String getBaseUrl()
    {
      return BASE_URL;

    }



    private Properties loadProperties() throws IOException
{
    InputStream testPropertiesInput = getClass().getClassLoader().getResourceAsStream("webtester.properties");
    Properties testProperties = new Properties();

    if (null != testPropertiesInput)
    {
        try
        {
            testProperties.load(testPropertiesInput);
        }
        finally
        {
            IOUtils.closeQuietly(testPropertiesInput);
        }
    }
    return testProperties;
}


   }
4

3 に答える 3

4

ここで何を尋ねているのかわかりませんが、提供したコードはコンパイルされません。交換

private static Properties testProperties;
testProperties = loadProperties();

private static final Properties testProperties = loadProperties();

更新、別のバグが見つかりました。のメソッド シグネチャも変更する必要がありloadPropertiesます。

private static final Properties loadProperties() throws IOException {...}

           ^     ^
于 2012-11-03T08:41:31.937 に答える
1

問題の現在の設計を使用すると、次の変更を行う必要があります。

public class WebDriverConfiguration {
    private static Properties testProperties = loadProperties();
    //...snip...

    private static Properties loadProperties() { //must be static and can not throw a checked exception
    //...snip...
    }
}
于 2012-11-03T08:50:49.423 に答える
0

私はあなたがこれをすべきだと思います:

まず、コンストラクターの署名を非公開にします。次に、loadProperties() を静的ブロックに配置します。第三に、すべてのプロパティを非公開にする必要があります。

于 2012-11-03T09:01:08.097 に答える