Spring Batch测试用例忽略Spring配置文件

时间:2017-11-08 14:54:20

标签: java spring-boot spring-batch

我正在为Spring Batch应用程序执行PoC。到目前为止一切正常,但我无法通过测试来获取Datasource,它始终会失败:

...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 42 more

这是我的application.yml文件:

server:
  port: ${port:9080}
spring:
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  output:
      ansi:
        enabled: detect
  profiles: # default, development, production
    active: default, development
---
spring:
  datasource:
    driver-class-name: org.h2.Driver
    sql-script-encoding: UTF-8
    url: jdbc:h2:mem:JavaSpringBootBatch;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL
    username: sa
    password:
  h2:
    console:
      enabled: true
      path: /h2-console
  profiles: development
---
server:
  port: ${port:80}
spring:
  datasource:
    initialize: false
    # ...
  profiles: production

...和我的测试类:

@Configuration
@Import(AppConfig.class) // This eventually "imports" the app's config
public class TestConfig {
  @Bean
  public JobLauncherTestUtils jobLauncherTestUtils() {
    return new JobLauncherTestUtils();
  }
}
@ActiveProfiles("development")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
//@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
public class PlayerItemBatchTest {
  @Autowired
  private JobLauncherTestUtils jobLauncherTestUtils;

  @Test
  public void readProcessWriteBatchProcess() throws Exception {
    final JobExecution jobExecution = jobLauncherTestUtils.launchJob();
    Assertions.assertThat(jobLauncherTestUtils.launchJob().getStatus()).isEqualTo(BatchStatus.COMPLETED);
  }
}
@Configuration
public class BatchConfig { // Included with AppConfig
  private final DataSource dataSource;

  @Autowired
  public BatchConfig(final DataSource dataSource) {
    this.dataSource = dataSource;
  }

  // ...
}

当我启动我的应用程序时,我可以清楚地看到datasource包含我在YAML的development个人资料中的所有内容(所有设置)。当我尝试执行测试用例时,它只是无法说明"它无法找到数据源"。

3 个答案:

答案 0 :(得分:1)

除了使用@EnableConfigurationProperties 尝试在test / resources中创建一个.yml文件,并使用@TestConfiguration注释在test / src中创建一个TestConfig.java。

答案 1 :(得分:1)

通常我会对 org.springframework.batch.core.configuration.annotation.BatchConfigurer 进行显式配置 将Spring Batch与Spring Boot结合使用时。这样可以提供更好的控制 - 您可以在此处找到一个示例:Spring Batch with Spring Boot Integration testing

答案 2 :(得分:0)

它似乎是一个依赖配置问题。您需要依赖spring-jdbc才能自动配置嵌入式数据库。

package Testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.Iterator;
import java.util.List;


public class Radiobuttons {


        public static void main(String[] args) 
    {
            // TODO Auto-generated method stub
            System.setProperty("webdriver.chrome.driver","C://chromedriver.exe");
            WebDriver driver=new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://jqueryui.com/droppable/");
            System.out.println(driver.getTitle());
            WebElement SimplePhotoManager = driver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/ul/li[5]/a"));
            SimplePhotoManager.click();
            driver.switchTo().frame(driver.findElement(By.xpath("//*[@id=\"content\"]/iframe")));
            WebElement source1 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[1]"));
            WebElement source2 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[2]"));
            WebElement source3 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[3]"));
            WebElement source4 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[4]"));
            WebElement targetBefore = driver.findElement(By.xpath("//*[@id=\"trash\"]"));
            Actions a = new Actions(driver);
            a.dragAndDrop(source1, targetBefore).build().perform();
            a.dragAndDrop(source2, targetBefore).build().perform();
            a.dragAndDrop(source3, targetBefore).build().perform();
            a.dragAndDrop(source4, targetBefore).build().perform();
            //Why is this required?
            List<WebElement> getClass= driver.findElements(By.className("a.ui-icon"));
            //Grab common attribute//Put into list and iterate
            int count=driver.findElements(By.className("ui-icon-refresh")).size();
            System.out.println(count);
            for(int i=0;i<count;i++)
            {
                driver.findElements(By.className("ui-icon-refresh")).get(i).click();
            }

            //Close the thing 
           //driver.quit();
    }


}

Here是可以帮助您的文档。