【问题标题】:NullPointerException when using bean in class method在类方法中使用 bean 时出现 NullPointerException
【发布时间】:2018-02-15 00:02:52
【问题描述】:

我有一个 Spring-boot 应用程序。 我想在类方法中使用 application.properties 中的变量,但我有 nullPointerException。

这是一个不起作用的简单示例。

application.properties:

#data paths
file.path=C:\\Users\\apodar\\autoTest

Config.java

package com.eserv.autotest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {

@Value("${file.path}")
String filePath;

    public String getFilePath() { return filePath; }
    public String getScreenshotsPath() {
        return getFilePath() + "/screenshots";
       }

}

AutotestApplication.java

package com.eserv.autotest;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.Transactional;


@SpringBootApplication(
        scanBasePackageClasses = {
            AutotestApplication.class,
       }
)
public class AutotestApplication implements CommandLineRunner {


    @Autowired DataSource dataSource;

    public static void main(String[] args) {
        SpringApplication.run(AutotestApplication.class, args);
    }

    @Transactional(readOnly = true)
    @Override
    public void run(String... args) throws Exception {

         System.out.println("DATASOURCE = " + dataSource);

    }
 }

SeleniumTestExecutionListener:

    public class SeleniumTestExecutionListener extends AbstractTestExecutionListener {

    @Inject Config config;

    private WebDriver webDriver;

    @Override
    public void afterTestMethod(TestContext testContext) throws Exception {
        if (testContext.getTestException() == null) {
            return;
        }
        File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        String testName = toLowerUnderscore(testContext.getTestClass().getSimpleName());
        String methodName = toLowerUnderscore(testContext.getTestMethod().getName());

        FileUtils.copyFile(screenshot, new File( config.getScreenshotsPath() + testName + "_" + methodName + "_" + screenshot.getName()));
    }

}

为什么config.getScreenshotsPath() 方法不返回路径。 config 为空。

【问题讨论】:

  • 你能提供用@SpringBootApplication注解的类吗?
  • 自动装配在 TestExecutionListener 中不起作用。
  • @MironBalcerzak 我用 SpringBootApplication 类编辑帖子。
  • SeleniumTestExecutionListener 不是@Component?然后 Spring 不关心它的自动装配。然后像配置这样的自动装配字段没有正确初始化。

标签: java spring-boot dependency-injection


【解决方案1】:

TestExecutionListener 中的自动装配将不起作用。 TestExecutionListener 实例的创建和生命周期由 Spring 的测试上下文框架管理,它不是 ApplicationContext 的一部分,而是外部的。因此自动接线将不起作用。

如果您想在 TestExecutionListener 中使用 bean,而不是从 TestContext 检索 ApplicationContext

@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    if (testContext.getTestException() == null) {
        return;
    }

    final Config config = testContext.getApplicationContext().getBean(Config.class);
    File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
    String testName = toLowerUnderscore(testContext.getTestClass().getSimpleName());
    String methodName = toLowerUnderscore(testContext.getTestMethod().getName());

    FileUtils.copyFile(screenshot, new File( config.getScreenshotsPath() + testName + "_" + methodName + "_" + screenshot.getName()));
}

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2015-04-16
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多