2017-09-06 14 views
-2

스프링 부트 응용 프로그램이 있습니다. 클래스 메서드에서 application.properties의 변수를 사용하려고하지만 nullPointerException이 있습니다.클래스 메서드에서 bean을 사용할 때 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이 null입니다.

+0

@SpringBootApplication으로 주석 된 클래스를 제공 할 수 있습니까? –

+0

Autowiring은'TestExecutionListener'에서 작동하지 않습니다. –

+0

[Spring @Value annotation의 중복 가능한 항목은 항상 null로 평가됩니까?] (https://stackoverflow.com/questions/4130486/spring-value-annotation-always-evaluating-as-null) –

답변

0

TestExecutionListener의 자동 와이어 링이 작동하지 않습니다. TestExecutionListener 인스턴스의 생성 및 수명주기는 Spring의 Test Context 프레임 워크에 의해 관리되며 ApplicationContext이지만 외부의 일부는 아닙니다. 따라서 자동 배선이 작동하지 않습니다.

TestExecutionListener에서 콩을 사용하려는 경우 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())); 
} 
관련 문제