You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven Profile无法获取site.url:Java+TestNG项目启动报错

Fixing the NullPointerException for site.url in Maven + TestNG Project

Got it, let's break down why you're hitting that NullPointerException and get your setup working properly.

Root Cause

There are two key issues here:

  1. Your code is trying to fetch site.url directly via System.getProperty("site.url"), but Maven's Profile property isn't being passed into the TestNG test's JVM system properties.
  2. Even though you enabled resource filtering to replace ${site.url} in environment.properties, your code doesn't actually read this properties file — it skips straight to system properties, which are empty.
  3. Bonus: Your POM has duplicate maven-compiler-plugin entries, which is unnecessary and messy.

Step-by-Step Fixes

You have two valid paths to resolve this; pick the one that fits your workflow better.

Option 1: Read the filtered environment.properties file in your code

Since you already set up Maven to replace the ${site.url} placeholder in environment.properties, update your test code to load this file and pull the value from it:

public void SetUp() {
    Properties envProps = new Properties();
    try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("environment.properties")) {
        envProps.load(inputStream);
        String siteUrl = envProps.getProperty("site.url");

        // Initialize driver and navigate
        System.setProperty("webdriver.chrome.driver", ConfigFileReader.getDriverPath());
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(siteUrl);
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    } catch (IOException e) {
        e.printStackTrace();
        // Add proper error handling here (e.g., throw a test initialization exception)
    }
}

Option 2: Inject the Profile property directly into test system properties

If you prefer to keep using System.getProperty("site.url"), modify your maven-surefire-plugin configuration to pass the Profile's site.url into the test JVM's system properties:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
        <suiteXmlFiles>
            <file>${project.basedir}/suites/smoke.xml</file>
        </suiteXmlFiles>
        <systemPropertyVariables>
            <environment.properties>/environment.properties</environment.properties>
            <!-- Add this line to pass the Profile property as a system property -->
            <site.url>${site.url}</site.url>
        </systemPropertyVariables>
    </configuration>
</plugin>

Now when you run mvn clean install -Pdevhost, the Surefire plugin will inject site.url into the test's system properties, and your existing System.getProperty("site.url") call will work as expected.

Additional Cleanup

  • Remove the duplicate maven-compiler-plugin entry from your POM — you only need one.
  • Use forward slashes (/) for resource directory paths (e.g., src/test/resources) instead of backslashes (\) to avoid cross-platform issues.

Verification

Run your command again:

mvn clean install -Pdevhost

Your test should now successfully retrieve the site.url value and navigate to the desired site without the NullPointerException.

内容的提问来源于stack exchange,提问作者Y_Sh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:13:56