-2

I have a Spring-boot app. I want to use variable from application.properties in class method but I have nullPointerException.

Here's a simple example that doesn't work.

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()));
    }

}

Why does config.getScreenshotsPath() method doesn't return path. config is null.

11
  • can you provide your class annotated with @SpringBootApplication? Commented Sep 6, 2017 at 13:14
  • Autowiring doesn't work in a TestExecutionListener. Commented Sep 6, 2017 at 13:16
  • Possible duplicate of Spring @Value annotation always evaluating as null? Commented Sep 6, 2017 at 13:22
  • @MironBalcerzak I have edit post with SpringBootApplication class. Commented Sep 6, 2017 at 13:28
  • SeleniumTestExecutionListener is not @Component? Then Spring doesn't care about its autowiring. Then autowired fields like config are not properly initialized. Commented Sep 6, 2017 at 13:30

1 Answer 1

0

Autowiring in a TestExecutionListener will not work. The creation and lifecycle of the TestExecutionListener instances is managed by the Test Context framework of Spring and that isn't part of the ApplicationContext but external. Hence auto wiring will not work.

If you want to use beans in your TestExecutionListener instead retrieve the ApplicationContext from the TestContext.

@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()));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.